LeetCode Β· HashMap
HashMap Problem Set
Complement lookup, canonical grouping, prefix-sum maps, and keyβnode design patterns. If the brute-force solution keeps rescanning for matches, a HashMap usually removes the inner loop.
Related pages: HashMap Β·
Arrays & Hashing
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | hash-map | LC 1 Β· Two Sum | Store previously seen values by number, then check whether target - nums[i] has already appeared. |
| Medium | grouping | LC 49 Β· Group Anagrams | Canonicalize each word, then use that canonical form as the map key to collect anagram groups. |
| Medium | frequency | LC 347 Β· Top K Frequent Elements | HashMap builds counts in one pass; a heap or bucket structure then extracts the top-k answers efficiently. |
| Medium | prefix-sum | LC 560 Β· Subarray Sum Equals K | Store how many times each prefix sum has appeared. If currentSum - k exists, every occurrence contributes one valid subarray ending here. |
| Medium | set+map-adjacent | LC 128 Β· Longest Consecutive Sequence | Usually solved with a HashSet, but it belongs in the broader hashing toolkit: O(1) membership checks let you expand only from sequence starts. |
| Medium | design | LC 146 Β· LRU Cache | HashMap gives O(1) lookup by key, while a doubly linked list gives O(1) eviction and move-to-front. You need both structures together. |
Practice note: Solution write-ups already exist for LC 1, 49, 347, and 128. LC 560 and LC 146 are intentionally listed here as canonical HashMap practice problems even though their dedicated solution pages have not been added yet.