Data Structures
Hash Table Collision Resolution
A hash table collision happens when two different keys land in the same slot. Every hash table needs a plan for it. The main plans are chaining and open addressing, and open addressing has three well-known upgrades: cuckoo, Robin Hood and hopscotch. Here is the comparison table first, then each strategy in depth.
At a glance
Two different keys can hash to the same slot. This is called a collision, and every hash table must handle it. The way it handles it decides how fast lookups are, how full the table can get, and how well it uses the CPU cache.
There are two big families. Chaining keeps a list of colliding keys in each bucket. Open addressing keeps every key in one array and probes (searches) for another free slot. Use the table below to compare them, then open each page for an interactive step-by-step visualization.
| Data Structures | Collision strategy | Lookup (expected) | Lookup (worst) | Insert | Load factor it tolerates | Memory / cache behavior | Best for |
|---|---|---|---|---|---|---|---|
| Hash Table with Chaining | Separate chaining: a list per bucket | O(1 + α), about O(1) at a normal load | O(n) with plain lists | O(1), then resize when α gets too high | Works above 1.0, but chains get longer; resizes usually trigger near 0.75 | Extra pointer per entry; nodes scattered in memory, so more cache misses | General-purpose maps, easy deletion, unpredictable load |
| Cuckoo Hashing | Open addressing with 2 hash functions and eviction | O(1) | O(1): at most 2 slot reads | O(1) expected; a rare insert triggers an O(n) rehash | About 50% with 2 hash functions; 90%+ with 4-slot buckets or more hash functions | Flat arrays, but the two probes hit two different cache lines | Read-heavy tables that need a hard lookup bound, such as network hardware |
| Hopscotch Hashing | Open addressing with a bounded neighborhood | O(1) | O(H): bounded by the neighborhood size | O(1) expected; moves keys so a free slot hops back toward home; resizes if no hop works | Stays fast above 90% | One flat array; the neighborhood is usually about one cache line, plus a small bitmap per bucket | High load factors and concurrent tables |
| Robin Hood Hashing | Open addressing (linear probing) with displacement balancing | O(1); same mean as linear probing | O(log n) expected longest probe at constant load | O(1) expected; may shift a run of keys | Runs well at 80-90% | One flat array; sequential probing is very cache-friendly | Predictable tail latency in dense, flat tables |
Each algorithm in depth
Click any card for the full article with an interactive visualization, complexity analysis, code examples, and FAQ.
-
Hash Table with Chaining
Each bucket holds a small list. Keys that collide are added to the same list. Simple and forgiving, but every list node is a separate pointer hop.
View interactive ↗ -
Cuckoo Hashing
Each key has two possible homes, one per hash function. A new key kicks out whatever sits in its slot, and the evicted key moves to its other home. Lookups check at most two slots.
View interactive ↗ -
Hopscotch Hashing
Every key must live within a small fixed neighborhood of H slots next to its home slot. A bitmap per bucket says which neighbors belong to it. Inserts shuffle keys to keep that promise.
View interactive ↗ -
Robin Hood Hashing
Linear probing with one extra rule. A key that has travelled far from home takes the slot of a key that has travelled less. This evens out probe lengths and shrinks the worst case.
View interactive ↗
What is a collision, and why can't you avoid it?
A hash function turns a key into a number. The table then uses that number to pick a slot. There are far more possible keys than slots, so some keys must share a slot. This is the pigeonhole principle: put 11 pigeons in 10 holes, and one hole holds two.
Even with fewer keys than slots, collisions show up early. The birthday paradox says that in a table with 365 slots, you expect your first collision after only about 23 random keys. So a collision plan is not optional. It is part of every hash table.
Chaining: a list in every bucket
Hash table chaining stores all keys that land in a bucket in a small list. To look up a key, hash it, then walk that one list. Insert is easy: add to the list. Delete is easy too: remove from the list.
Chaining degrades gently. If the table gets too full, lists just get longer. It never runs out of room. The cost is memory and speed: each list node needs a pointer, and nodes can sit far apart in memory.
Open addressing: probe for another slot
In open addressing, there are no lists. Every key sits directly in the array. If the slot you want is taken, you probe: you check the next slot, then the next, until you find a free one. A lookup follows the same probe path. It stops when it finds the key or hits an empty slot.
The simplest version is linear probing: just try the next slot. It is fast, but it suffers from clustering. Occupied slots merge into long runs, and any key that hashes into a run has to scan all of it. Cuckoo, Robin Hood and hopscotch hashing are three ways to fix this problem.
- Cuckoo hashing gives each key two possible slots. A lookup checks both and stops. Worst-case lookup is always two reads. The price is that an insert can kick keys around in a loop, which forces a rebuild of the whole table.
- Robin Hood hashing keeps linear probing but lets a key that is far from home take the slot of a key that is close to home. The average probe length stays the same. The spread shrinks, so the unlucky keys are far less unlucky. A lookup can also stop early once it passes the point where its key would have been.
- Hopscotch hashing promises that every key lives within H slots of its home. A small bitmap per bucket points at the right slots. Inserts move keys around to keep the promise. If they cannot, the table grows.
Load factor and resizing
The load factor, written α, is the number of keys divided by the number of slots. A table with 750 keys in 1,000 slots has α = 0.75.
Load factor is the main dial. In chaining, the average list length is α, so lookups cost about 1 + α. In open addressing, α cannot exceed 1, and probe lengths blow up as α nears 1. For plain linear probing, the expected probes for a failed search grow like 1/(1 − α)². At α = 0.9 that is about 50 probes. That is why open-addressing tables resize early.
To resize, the table allocates a bigger array, usually double, and re-inserts every key. This costs O(n) once, but it happens rarely, so the average cost per insert stays O(1). Java's HashMap resizes at α = 0.75 by default. Cuckoo tables also rehash when an insert cannot find a home.
The cache argument: flat arrays versus pointer chasing
A modern CPU reads memory in cache lines of 64 bytes. Reading the next slot in an array is nearly free, because it is probably already in the cache. Following a pointer to a list node can cost a full cache miss, which is far slower than a cache hit.
Open addressing keeps keys in one flat array, so probing walks through memory in order. That is why open-addressing tables usually beat chained tables on raw speed, even when they do a few more comparisons. The trade-off: open addressing needs a lower load factor to stay quick, and deletion is trickier. Removing a key can break the probe path of other keys, so tables use tombstone markers, or Robin Hood's backward shift, to repair it.
Where each one shows up in practice
- Chaining: Java's
HashMapuses buckets with linked lists, and it converts a very long list into a balanced tree. C++'sstd::unordered_mapis also chained. Chaining is easy to get right, and its references to stored items stay valid across resizes. - Open addressing: Rust's standard
HashMapis built on hashbrown, a port of Google's Swiss table. It is an open-addressing design that checks a group of slots at once with SIMD instructions. Python'sdictalso uses open addressing, with a perturbed probe sequence. - Robin Hood: Rust's standard map used it until it switched to hashbrown in 2019. Libraries such as
tsl::robin_mapin C++ still use it for dense, fast tables. - Cuckoo: Common in network hardware and packet-processing software, where a lookup must finish in a fixed number of memory reads. Cuckoo-style tables show up in switches and in software packet-processing libraries.
- Hopscotch: Used in research and in concurrent hash maps, where a bounded neighborhood means a thread only needs to lock a small window.
Decision guide: which one should you pick?
If you are using a language's built-in map, you do not choose. Use it. The guide below is for when you build your own table or need to understand a design.
- Not sure, or load is unpredictable, or you delete a lot: use chaining. It is simple and degrades gently.
- You want speed and a compact table, and keys are small: use open addressing with linear probing, ideally with Robin Hood balancing.
- You need steady tail latency in a dense table: use Robin Hood hashing.
- Lookups vastly outnumber inserts, and you need a hard worst-case bound: use cuckoo hashing.
- You need very high load factors or many threads sharing the table: use hopscotch hashing.
For a different problem, spreading keys across many servers, see the separate family of consistent hashing methods. They do not resolve collisions inside one table.
Frequently asked questions
What is hash table collision resolution?
It is the method a hash table uses when two different keys hash to the same slot. The two main families are chaining, which stores colliding keys in a list inside the bucket, and open addressing, which probes for another free slot in the same array.
What is the difference between chaining and open addressing?
Chaining stores colliding keys in a separate list per bucket, so the table can hold more keys than it has buckets. Open addressing stores every key directly in the array and probes for another free slot, so it can never hold more keys than slots. Open addressing is usually more cache-friendly. Chaining is simpler and handles high load and deletion more gracefully.
Which is faster, chaining or open addressing?
In practice open addressing is usually faster, because it reads a flat array and gets far fewer cache misses than chasing list pointers. The gap shrinks or reverses when the table is very full, because probe sequences get long, or when entries are large.
Cuckoo hashing vs Robin Hood hashing: which should I use?
Use cuckoo hashing when you need a hard guarantee that a lookup touches at most two slots, such as in hardware or latency-critical read paths. Use Robin Hood hashing when you want a simple, cache-friendly table with low variance in lookup time. Robin Hood inserts do not fail and trigger a full rebuild the way cuckoo inserts can, and Robin Hood tolerates a much higher load factor than basic two-function cuckoo.
What is load factor and why does it matter?
Load factor is the number of stored keys divided by the number of slots. It controls how often collisions happen. As it rises, chains get longer or probe sequences grow, so lookups slow down. Tables resize, usually doubling, when the load factor passes a limit such as 0.75.
Why can't hash collisions be avoided completely?
There are far more possible keys than table slots, so by the pigeonhole principle some keys must share a slot. A perfect hash function can avoid collisions, but only when the full key set is known in advance. For a table that grows and changes, you need a collision plan.
How does deletion work in open addressing?
You cannot simply empty the slot, because a lookup for another key might need to probe through it and would stop too early. Most tables mark the slot with a tombstone, which means deleted but keep probing. Robin Hood hashing instead shifts the following keys back by one slot, so no tombstones are needed.
Which collision strategy do Java, Python and Rust use?
Java's HashMap uses chaining, with long chains converted into balanced trees. Python's dict uses open addressing. Rust's standard HashMap uses hashbrown, an open-addressing Swiss table design. Rust's older map used Robin Hood hashing until 2019.