Hash Maps
The most powerful general-purpose data structure ever invented. Average O(1) insert, delete, and lookup — regardless of how many items you store. Understanding hash maps separates novice from experienced engineers.
What is a hash map?
A hash map (hash table) stores key–value pairs and finds any value by its key in O(1) on average. It hashes the key to an array index, so it doesn't need to search. In JavaScript, use Map (any key type, keeps insertion order) or a plain object for string keys. Hash maps are the single most useful structure in interviews: counting, caching, deduplication and lookups.
const ages = new Map();
ages.set("Ada", 36);
ages.get("Ada"); // 36 O(1) average
ages.has("Bob"); // false
ages.delete("Ada");
// Two Sum in O(n) with a hash map
function twoSum(nums, target) {
const seen = new Map();
for (const [i, n] of nums.entries()) {
if (seen.has(target - n)) return [seen.get(target - n), i];
seen.set(n, i);
}
}The core idea
Arrays give O(1) access by integer index. A hash map extends that to any key type — strings, objects, anything — by transforming the key into an integer index via a hash function.
Your key
Integer index
Bucket
Average lookup regardless of stored count
Typical maximum load factor before resize
Capacity multiplier on resize (like dynamic arrays)
Live demo — hash, bucket, and collision
Type a key and value then hit Set. Watch which bucket the hash function selects and see collisions appear when two keys land in the same slot.
Hash Map — 8 buckets · h(k) = polyHash(k) % 8
entries: 0Interactive sandbox — code a Hash Map
Implement a rolling hash algorithm and chain-based collision resolution using singly linked nodes. Insert, delete, and lookup keys instantly!
Code Editor
<input id="k" placeholder="key" value="apple">
<input id="v" placeholder="value" value="1.99">
<button id="set">set</button> <button id="get">get</button> <button id="del">delete</button>
<pre id="out"></pre>Preview
Code playground — build it yourself
Test separate chaining manually. This implementation splits your collisions directly into array lists underneath bucket addresses:
Code Editor
<pre id="out"></pre>Preview
Core operations
1HashMap: array of buckets, each bucket is a list of (key, value) pairs23put(key, value):4 index <- hash(key) % buckets.length5 for each pair in buckets[index]:6 if pair.key = key: pair.value <- value; return // update7 buckets[index].append((key, value)) // insert8 if load_factor() > 0.75: resize(capacity x 2)910get(key):11 index <- hash(key) % buckets.length12 for each pair in buckets[index]:13 if pair.key = key: return pair.value14 return null // not found1516delete(key):17 index <- hash(key) % buckets.length18 remove pair where pair.key = key from buckets[index]
Collision resolution
Two different keys can produce the same index. This is called a collision, and every hash map must handle it. There are two dominant strategies:
Separate chaining
Java HashMap, Python dict (legacy)Each bucket holds a linked list. Colliding entries are appended to the list. Lookup scans the list. Simple and handles high load factors gracefully.
Open addressing (linear probing)
Python dict (modern), many embedded systemsWhen a collision occurs, search the next empty slot. Lookup follows the same probe sequence. Cache-friendly but degrades under high load (clustering).
Load factor and resizing
Load factor = items stored / total buckets. As load factor grows, collisions increase and performance degrades. Most implementations resize (double capacity) when load factor exceeds 0.75.
Time complexity
Constant time
Constant time
Constant time
Linear time
Linear time
🎯 Quick Check
You insert 100 items into a hash map with 128 buckets. Load factor reaches ~0.78, triggering a resize to 256 buckets. What happens to existing keys during resize?
Hash Maps: summary
Key points
- Average O(1) for get, set and delete; the worst case is O(n) when many keys collide.
- Prefer
Mapover objects for frequent additions and removals, or for non-string keys. - Replacing a nested loop with a hash map lookup often turns O(n²) into O(n).
Frequently asked questions
›Map vs object in JavaScript?
Map accepts any key type, keeps insertion order, has .size, and is optimised for frequent changes. Objects are fine for fixed records with string keys and JSON.
›Why is hash map lookup O(1)?
The key is hashed directly to an array index, so the map jumps to the right bucket instead of scanning entries.