Skip to content

Hash Map

Medium — good to knowGeneral Dev

ELI5 — The Vibe Check

A hash map is a super-fast lookup table. You store a value under a key (like a label), and you can find it instantly without searching through everything. It is like a dictionary — you do not read every page to find 'apple', you jump straight to 'A'. In JavaScript, plain objects and the Map class are hash maps.

Real Talk

A hash map (also called hash table, dictionary, or map) is a data structure that maps keys to values using a hash function. The hash function converts a key into an array index, enabling average O(1) lookup, insertion, and deletion. Collisions (two keys with the same hash) are handled by chaining or open addressing. In JavaScript, plain objects and Map serve as hash maps.

Show Me The Code

// JavaScript Map (true hash map):
const cache = new Map();
cache.set('user:123', { name: 'Alice' }); // O(1) insert
const user = cache.get('user:123');        // O(1) lookup
cache.has('user:999');                     // false

// Plain object as hash map:
const index = {};
index['alice@email.com'] = userId;

// Use case: counting word frequency
const freq = {};
for (const word of words) {
  freq[word] = (freq[word] ?? 0) + 1;
}

When You'll Hear This

"Use a hash map to cache the results." / "A hash map gives you O(1) lookup instead of scanning the whole array."

Made with passive-aggressive love by manoga.digital. Powered by Claude.