Visualize how Chaining handles collisions using linked lists at each bucket index. Step-by-step logic for data insertion and retrieval.
About this algorithm
A hash map stores key and value pairs so that finding a key takes about the same time however many keys there are. A hash function turns the key into a bucket number, and the pair is stored in that bucket. With separate chaining, each bucket holds a small list, so two keys that land in the same bucket sit one after the other in that list.
Chaining is the easiest collision strategy to get right, and it keeps working when the table gets crowded: a bucket just grows a longer list. Java's HashMap works this way. Pick linear probing instead when you want everything in one flat array with no extra list nodes, which is kinder to the CPU cache, but then you must keep the table well under full and deletion gets harder. If you only need to count things or remember what you have seen, the valid anagram and two sum pages show a map being used rather than built.
Run the key through the hash function and take the remainder by the number of buckets. On this page the function walks the key's characters, multiplying the running total by 31 and adding each character code, and keeps everything mod the table size.
Walk the list in that bucket comparing keys. If the key is already there, overwrite its value. If you reach the end without a match, add a new entry to the end of the list.
Hash the key, go to its bucket and compare it with each entry in the chain. A match returns the value; reaching the end of the chain means the key is not in the map. No other bucket is ever looked at.
Find the entry the same way and remove it from its chain. Nothing else in the table moves, which is the big advantage over open addressing.
The page starts with an empty table of 7 buckets and you type the keys. Insert apple, banana, cherry and date (values 12, 24, 36, 48). For date the hash goes: d gives 100 mod 7 = 2, then (2 × 31 + 97) mod 7 = 5 for a, then 5 for t, then 4 for e, so date lands in bucket 4. Apple lands in bucket 1 and banana in bucket 3. Cherry also hashes to 3: that is a collision, and cherry is added after banana, so bucket 3 now reads banana → cherry. Searching for cherry compares with banana, then with cherry, and returns 36 after two comparisons. Inserting cherry again with 99 finds the existing entry and updates its value instead of adding a duplicate.
Divide the number of keys by the number of buckets and you get the load factor, the average chain length. Real maps grow the table and rehash every key once it passes a limit (Java uses 0.75), which keeps chains short. This page never grows the table, so if you fill 7 buckets with 20 keys you can watch chains get long and lookups slow down.
Time: with a good hash and a bounded load factor each chain holds a few entries, so operations are O(1) on average; if every key hashes to one bucket, that chain has all N keys and a lookup is O(N). Space: M buckets plus one list entry for each of the N keys.