Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Hash collision

Коллизия хешей

When a hash function gives the same result for two different inputs, that's a collision. A hash table must still be able to tell those inputs apart.

Also known as: коллизия

Why hashes can match

A hash function maps a key, such as a string, to a number. A 32-bit hash has only 2^32 possible results. If the set of possible keys is larger than that, some pair of distinct keys must share a hash. This is the pigeonhole principle. It doesn't mean every set of keys contains a collision, or that a collision is a bug. For a hash table, a good hash function spreads keys evenly enough that lookups stay fast.

A hash isn't a bucket index

A hash table typically maps a hash to a position in a backing array. With eight buckets and the rule index = hash % 8, hash values 6 and 14 both lead to bucket 6. That's a bucket collision, even though the full hash values differ. Two keys with exactly the same hash also start at the same bucket. In either case, the table needs a way to store both keys and later retrieve the right one.

How a lookup finds the right key

Separate chaining lets each bucket hold several entries. Suppose red and blue land in bucket 3. A lookup for blue checks entries in that bucket until it finds the matching key. An insertion for blue updates its entry if it already exists; it doesn't overwrite red just because the bucket is shared.

Open addressing stores entries directly in the array. If the starting slot holds a different key, the table follows a probing rule to try other slots. For example, linear probing checks adjacent slots in order, wrapping around at the end. Lookup follows the same sequence as insertion. Deleting an entry often requires leaving a marker: clearing the slot outright could make a later lookup stop before reaching a key farther along the sequence.

Where it matters

Dictionaries, sets, and hash-based caches use these techniques to make collisions a performance concern rather than a correctness problem. With well-distributed hashes and a controlled load factor (the number of entries divided by the number of buckets or slots), a lookup takes expected O(1) time, assuming hashing and key comparisons take constant time. In a simple chained table, lookup can take O(n) if all n entries land in one bucket and the search has to scan them.

How it comes up in interviews

A common follow-up is whether expanding the table fixes collisions. Resizing can separate keys whose different hashes previously mapped to the same bucket. But if the hash function and its outputs stay the same, two keys with identical hashes still have the same starting position. Another common question is about the equality contract: equal keys must produce equal hashes, but equal hashes do not prove that the keys are equal.