Data Structures & Algorithms / Module 2: Hashing
Topic 2.2
Hash maps and collisions
Turning a name into a drawer number so data is found straight away, and what happens when two names want the same drawer.
Watch: type a name, then press set or get. The item goes straight into its drawer or comes straight out, without checking the others.
fnv1a(key) = 2779197821 · 2779197821 % 8 = 5
Like the lockers at a swimming pool
Picture lockers with one simple rule: your name is worked out into a number, and your things always go in the locker with that number. When you come back, your name is worked out the same way again, and you get the same number. No need to open the lockers one by one.
In a program the words are:
- key: the name you look things up by, such as
"apple". - value: what is stored, such as its price.
- bucket: the locker.
- hash function: the "formula" that turns a key into a locker number. The one rule: the same key always gives the same number.
Type a key under the figure. You can see the number it works out to and which locker it points at.
When two names get the same locker
There are only so many lockers, but endless possible names. So sometimes two names get the same number. That is a collision. In the figure, apel (apple) and pisang (banana) both land in locker 5.
The fix is simple: one locker may hold several things, and when you search, you check that locker's contents one by one. As long as each locker holds only one or two things, lookups stay almost instant.
The trouble starts with a bad formula. Pick the bad hash function in the figure: it only looks at the length of the name, so every name with the same number of letters piles into one locker. Searching turns back into checking one by one, and the hash map loses its advantage.
Using one in code
In JavaScript a hash map comes ready-made as Map (and Set when you only need the keys). You never write the hash function yourself.
Code, for programmersStore, get, check, delete: all almost instant
const price = new Map();
price.set("apple", 12);
price.set("orange", 9);
price.get("apple"); // 12
price.has("durian"); // false
price.delete("orange");Code, for programmersCounting how often each word appears, in a single pass
function countWords(text) {
const counts = new Map();
for (const word of text.split(/\s+/)) {
counts.set(word, (counts.get(word) ?? 0) + 1);
}
return counts;
}Where hash maps are used
- Caches: keeping results you already worked out so you don't work them out again.
- Removing duplicates: a
Setkeeps only one copy of each. - Matching data, such as finding each article's author by id without going back and forth to the database.
- Spreading data over servers: a hash function decides which server keeps which data.
Quiz
Why can a hash map find data almost instantly?
What happens if every key lands in the same locker?
8 lockers hold 12 things. What is the average per locker (the load factor)?
Code finds each order's customer with customers.find(...) inside the loop over orders. Best fix?
0 of 4 answered.