Hash Tables & Hash Maps Guide
479 words · Reviewed for accuracy

If I could give a beginner one habit, it'd be this: the second a problem whispers "have I seen this before?" or "how many times does this appear?", reach for a hash map. It's the single most useful reflex in coding interviews, and it's the reason so many O(n²) brute forces collapse into a clean O(n) pass.
Why it's everywhere: a hash map gives you average O(1) lookup, insert, and delete. That constant-time membership check is what powers the famous Two Sum trick and a hundred of its cousins.
The Two Sum insight, made explicit
// Brute force is O(n^2). One hash map makes it O(n):
function twoSum(nums, target) {
const seen = new Map(); // value → index
for (let i = 0; i < nums.length; i++) {
const need = target - nums[i];
if (seen.has(need)) return [seen.get(need), i];
seen.set(nums[i], i);
}
return [];
}
Read that pattern until it's obvious: "for each element, have I already seen the thing that completes it?" That question, in various disguises, is a shocking amount of the medium tier.
The patterns worth naming
- Complement lookup — Two Sum and its relatives.
- Frequency counter — anagrams, majority element, "first unique character."
- Grouping — bucket anagrams by their sorted signature.
- Window + map — track what's inside a sliding window for substring problems.
- Set for dedup — O(1) "seen it?" checks, cycle detection.
A frequency example
// Are two strings anagrams? Count, then compare.
function isAnagram(a, b) {
if (a.length !== b.length) return false;
const count = {};
for (const ch of a) count[ch] = (count[ch] || 0) + 1;
for (const ch of b) {
if (!count[ch]) return false; // missing or already used up
count[ch]--;
}
return true;
}
The collision question
A sharp interviewer may ask how a hash map handles collisions. Know the two families: chaining (a little list at each bucket — how Java's HashMap works) and open addressing (probe for the next open slot — closer to Python's dict). And know the catch: if everything hashes to one bucket, you're back to O(n). Naming that worst case unprompted looks great.
Common mistakes
- Using a hash map when the input is already sorted and two pointers would be simpler and O(1) space.
- Forgetting that keys must be hashable/immutable — you can't key on a mutable list.
- Quoting O(1) as if it's guaranteed. It's average case; say so.
FAQ
Map or set? Set when you only care whether something exists; map when you need to store a value alongside the key (like an index or a count).
Is a hash map always the best move? No. If the data is sorted, two pointers often beats it on space. The reflex is a starting point, not a law.
Hash maps are the Swiss Army knife of data structures and pair beautifully with array patterns. Practise with Aissence's coding copilot.
Put this into practice
Continue with the Aissence workflow this guide supports.