Overview
A hash map stores key-value pairs with O(1) average lookup; perfect for frequency counting.
Analogy
A library catalog: look up a book title instantly — no need to scan every shelf.
Step-by-step
- Create: d = {} or d = defaultdict(int).
- Update count: d[key] = d.get(key, 0) + 1.
- Lookup: d[key] (raises KeyError if missing) or d.get(key, default).
- Iterate: for k, v in d.items().
Visual
['a','b','a'] → {'a':2,'b':1} → d['a']=2
Common mistakes
- KeyError on missing key — always use .get() or defaultdict.
- Modifying dict while iterating raises RuntimeError.
Practice questions
- Find the most common element in a list.
- Two-sum using a hash map (O(n)).
Time
O(1) average get/set, O(n) iterate