šļø Hashing (Map / Set)
One-line summary: Use a hash table to achieve O(1) average-time lookup, insertion, and deletion ā turning O(n²) brute-force into O(n).
What is Hashing?ā
Key Hash Table Componentsā
- Hash Function: Converts keys into array indices
JavaScript Hash Structuresā
- Map (
new Map()) ā Key-value pairs; preserves insertion order; any type as key - Set (
new Set()) ā Unique values only; no duplicates allowed - Object (
{}) ā String/Symbol keys; prototype chain considerations - WeakMap/WeakSet ā Garbage collection friendly; object keys only
Performance Characteristicsā
- Average case: O(1) insert, lookup, delete
- Worst case: O(n) due to hash collisions (rare with good hash function)
- Space complexity: O(n) where n is number of elements
When to Use Hashing?ā
ā Perfect for:
- Fast lookups (checking if element exists)
- Counting frequencies (character/word counting)
- Removing duplicates (using Set)
- Caching/Memoization (storing computed results)
- Database indexing (quick record retrieval)
- Two-sum type problems (complement lookup)
ā Not suitable for:
- Ordered data (use TreeMap/sorted structures)
- Range queries (use segment trees/arrays)
- Memory-constrained environments (overhead of hash table)
- Small datasets (array iteration might be faster)
š Visual Learningā
Hash Table Structureā
Understanding how hash functions map keys to array indices and handle collisions
Step-by-step visualization of key hashing, collision detection, and resolution strategies
Comparing array-based storage vs hash table organization for efficient data access
Time & Space Complexityā
| Operation | Average | Worst |
|---|---|---|
| Insert | O(1) | O(n) |
| Lookup | O(1) | O(n) |
| Delete | O(1) | O(n) |
| Space | O(n) | O(n) |
Common Patternsā
Pattern 1 ā Frequency Countā
const freq = new Map();
for (const ch of s) freq.set(ch, (freq.get(ch) || 0) + 1);
Pattern 2 ā Complement Lookup (Two Sum)ā
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const comp = target - nums[i];
if (seen.has(comp)) return [seen.get(comp), i];
seen.set(nums[i], i);
}
Pattern 3 ā Canonical Key (Group Anagrams)ā
const groups = new Map();
for (const w of words) {
const key = w.split('').sort().join('');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(w);
}
Pattern 4 ā Set Membership (Contains Duplicate)ā
const seen = new Set();
for (const n of nums) {
if (seen.has(n)) return true;
seen.add(n);
}
return false;
Pitfallsā
- Using object
{}as map ā breaks with keys like"__proto__"ā prefernew Map() - Comparing object keys by reference, not value
- Forgetting that
Map.sizeāObject.keys(obj).length
Practice Problemsā
| Problem | Difficulty | Solution |
|---|
| LC 1 ā Two Sum | Easy | View Solution | | LC 217 ā Contains Duplicate | Easy | View Solution | | LC 242 ā Valid Anagram | Easy | View Solution | | LC 929 ā Unique Email Addresses | Easy | View Solution | | LC 575 ā Distribute Candies | Easy | View Solution | | LC 349 ā Intersection of Two Arrays | Easy | View Solution | | LC 219 ā Contains Duplicate II | Easy | View Solution | | LC 383 ā Ransom Note | Easy | | | LC 387 ā First Unique Character in a String | Easy | | | LC 389 ā Find the Difference | Easy | | | LC 448 ā Find All Numbers Disappeared in an Array | Easy | | | LC 771 ā Jewels and Stones | Easy | | | LC 1002 ā Find Common Characters | Easy | | | LC 1207 ā Unique Number of Occurrences | Easy | | | CC ā Frequency of Characters (FREQ) | Easy | | | CC ā Count Distinct Elements (DISTELEM) | Easy | | | LC 49 ā Group Anagrams | Medium | View Solution | | LC 347 ā Top K Frequent Elements | Medium | View Solution | | LC 128 ā Longest Consecutive Sequence | Medium | View Solution | | LC 167 ā Two Sum II | Medium | View Solution | | LC 3 ā Longest Substring Without Repeating Characters | Medium | | | LC 36 ā Valid Sudoku | Medium | | | LC 187 ā Repeated DNA Sequences | Medium | | | LC 454 ā 4Sum II | Medium | | | LC 560 ā Subarray Sum Equals K | Medium | | | LC 692 ā Top K Frequent Words | Medium | | | LC 974 ā Subarray Sums Divisible by K | Medium | | | LC 1010 ā Pairs of Songs With Total Durations Divisible by 60 | Medium | | | CC ā Subarray with Given Sum (SUBSUM) | Medium | | | CC ā Hash Table Operations (HASHTBL) | Medium | | | LC 30 ā Substring with Concatenation of All Words | Hard | | | LC 41 ā First Missing Positive | Hard | | | LC 149 ā Max Points on a Line | Hard | | | LC 269 ā Alien Dictionary | Hard | | | LC 336 ā Palindrome Pairs | Hard | | | CC ā Advanced Hashing (ADVHASH) | Hard | |
Related Topicsā
- Prefix Sum ā hash maps power "subarray sum = K"
- Sliding Window ā freq maps track window contents
- Two Pointers