Whenever a problem asks "does X exist" or "how many times have I seen Y" or "what's the count of each value," reach for a hash map. The pattern shows up constantly.
Two Sum
Given an array and a target, find two indices that sum to the target. Brute force is . With a hash map: as you scan, for each , check whether is already in the map. If yes, return the pair. If no, store and continue. time, space.
First non-repeating character
Count occurrences in one pass, then scan the string in order and return the first character whose count is 1. time, space.
Subarray sum equals K
Compute running prefix sum . At each step, look up how many times has appeared. That count equals the number of subarrays ending at the current index with sum . Increment the count of in the map. time.
Group by key
Anagrams, words with the same length, files with the same checksum — all the same pattern. Compute a canonical key per item, push each item into a list under its key.
Caches
Hash maps are the data structure underneath LRU caches. The map provides lookup; a doubly linked list maintains the eviction order. Each map value points to the corresponding list node so you can move-to-front in .
When NOT to use a hash map
When keys have a small range (use an array), when you need ordered iteration (use a tree map), when memory is extremely tight (hash maps have 30-50% overhead even when full).