A hash function sends every key to a slot, but sooner or later two keys want the same slot. This page explains why that is unavoidable, shows five standard ways to handle it with diagrams, and then lets you try each one live.
A hash table stores key/value pairs in an array. A hash function turns each key into an index, so a lookup can go straight to the right slot in O(1) time instead of scanning the whole array in O(n).
There are far more possible keys than slots: every possible name, but only a few hundred array positions. So some different keys must end up with the same index. When a new key hashes to a slot that is already taken, that is a collision, and keys that share a slot are called synonyms. Even a very good hash function only makes collisions rare; it cannot prevent them.
Every strategy below falls into one of two families:
The diagrams all use the same seven keys, 50, 700, 76, 85, 92, 73, 101, in a table of 7 slots with h(k) = k mod 7. Three of them (50, 85, 92) hash to slot 1, and two (73, 101) hash to slot 3.
Each slot points to a linked list of every key that hashed there. Insert adds a node at the front of the list; search walks the list comparing keys. The table never fills up: it just gets longer lists.
With n keys in m slots, the average list has n / m nodes (the load factor α), so search takes O(1 + α) on average.
This is the design in the class HashBucketsWithChaining code. Each slot is a bucket that holds up to B entries in a small array. Only when the bucket is full do extra entries go into an overflow linked list.
Most keys are found in the fast, contiguous bucket, and the linked list is only used for unlucky slots. Databases and file systems use buckets like this because one bucket fits in one disk block.
Open addressing in its simplest form: if slot h is taken, try the next slot, wrapping around at the end. Search follows the same sequence and stops at the key or at an empty slot.
The weakness is primary clustering: occupied slots form runs, any key that lands in a run has to walk to its end, and that makes the run longer still.
Instead of stepping one slot at a time, jump by successive squares. Keys that collide spread out quickly instead of piling into one run.
This removes primary clustering. Keys with the same home slot still follow the same path (secondary clustering). The probe sequence does not visit every slot: if m is prime and the table is at most half full, a free slot is always found; past that, an insert can fail even though empty slots remain.
On a collision, apply a different hash function to decide where to go next. The usual form is double hashing: a second hash function h₂ gives each key its own step size.
R is a prime smaller than m (here R = 5), and h₂ is never 0. Two keys with the same home slot usually have different step sizes, so they follow different paths. That removes secondary clustering too.
"Rehashing" also names something else: when a table gets too full, allocate a bigger array and re-insert every key. Both ideas matter; this section is about the first.
| Strategy | Where colliding keys go | Can α exceed 1? | Clustering | Deleting |
|---|---|---|---|---|
| Separate chaining | A linked list in the same slot | Yes | None; long lists instead | Unlink the node |
| Buckets with chaining | The bucket, then an overflow list | Yes | None; overflow lists instead | Remove from bucket or list |
| Linear probing | The next free slot | No | Primary (long runs) | Leave a tombstone |
| Quadratic probing | Slots h + 1, 4, 9, … | No (keep α ≤ 0.5) | Secondary only | Leave a tombstone |
| Double hashing | Slots h + i · h₂(k) | No | Very little | Leave a tombstone |
Pick a strategy, then insert, search for or delete keys. Keys can be whole numbers (hashed as k mod m) or words (hashed with the djb2 string hash from the class code). The probe badges show the order in which slots were checked.
This inserts one list of keys into each strategy at once and measures the result. "Average probes" is how many slots or nodes a successful search looks at, averaged over every key.