Interactive Hash Map visualization. Learn about Chaining, Open Addressing, and Hash Functions with real-time insertions and deletions.
About this algorithm
A hash map, also called a hash table or dictionary, stores values under keys and finds a value from its key without looking through every entry. It does this with a hash function, which turns any key into a whole number, and an array of buckets: that number, taken mod the array length, says which bucket the key belongs in. Put, get and remove all begin the same way, by working out that index.
Use a hash map whenever you look things up by name or ID: counting how often each word appears, caching results already computed, remembering which values you have met, or grouping records by a field. Python's dict, JavaScript's Map and Java's HashMap are all hash maps. Pick a sorted structure such as a binary search tree instead when you need keys in order or range queries like "every key from 10 to 20", because hashing scatters keys with no order. Two different keys can get the same index, a collision, and every hash map needs a rule for it: the chaining page keeps a list per bucket (this page uses that rule too), and the linear probing page moves the key along to the next free slot. The two sum page shows the classic interview use, a map of values seen so far.
This page reads the key one character at a time. It starts from 0, multiplies the running number by 31, adds the character code, and takes the result mod the table size after every character so the number never grows large. What is left, from 0 to size minus 1, is the bucket index. The same key always gives the same index, which is what lets a later lookup find it.
Compute the index, then look in that bucket. If the key is already stored there, replace its value, since a map holds each key at most once. If not, add the new pair to the bucket.
Compute the index and check only that bucket. Finding the key returns its value. Not finding it means the key is absent: no other bucket could hold it, because the hash would have sent it here.
Compute the index, find the key in that bucket and take it out. The other buckets are never opened.
The page opens on an empty table of 7 buckets; type a key and a value, then press Insert. Hashing cat: c is 99, and 99 mod 7 = 1. Then a: 1 × 31 + 97 = 128, and 128 mod 7 = 2. Then t: 2 × 31 + 116 = 178, and 178 mod 7 = 3, so cat goes to bucket 3. Dog hashes to 6 and owl to 1. Now ant: a gives 97 mod 7 = 6, n gives 6 × 31 + 110 = 296, which is 2 mod 7, and t gives 178 mod 7 = 3 again. Ant and cat are different keys with the same index, a collision, so bucket 3 holds both. Search for fox: it hashes to 6, the page compares it with dog, the only entry there, and reports not found without visiting any other bucket. Delete cat: bucket 3 is opened, cat is taken out and ant stays. Finally drag the table size slider to 11. The table empties, and inserting cat and ant again puts them in buckets 10 and 9: with a different size, the same keys spread apart.
Computing the index costs time in proportion to the key length, not to the number of keys, and after that only one bucket is checked. If the hash spreads N keys evenly over M buckets, each bucket holds about N / M of them, so a lookup inspects a handful of entries however large the map grows. The cost climbs only when many keys pile into a few buckets.
N / M is the load factor; the four keys above in 7 buckets give about 0.57. Library maps keep it under a fixed limit by allocating a bigger bucket array, often twice the size, and inserting every key again, because each index depends on the table size. One resize costs O(N), but doubling makes resizes rare enough that the cost per insert averages out to O(1). This page never resizes by itself: the slider runs from 3 to 20 and moving it starts a fresh empty table, so you can fill a small one and watch buckets crowd.
It must be deterministic, and it should spread similar keys apart. A function that only added up character codes would send every anagram, such as ant and tan, to one bucket; multiplying by 31 before each addition makes the order of the letters count, though any two keys can still collide by chance.
Time: each operation hashes the key and searches one bucket, which holds about N / M keys when the hash spreads them evenly, so it is O(1) on average; if every key lands in the same bucket, one lookup has to check all N. Space: an array of M buckets plus one stored entry for each of the N keys.