A hash map stores values by key and supports fast lookup in typical cases. In Python, the built-in dict is the standard hash map.
Hash maps are one of the most common ways to replace repeated scanning with direct lookup.
Core Idea
The algorithm chooses a key that identifies what it needs to find later. The map stores the answer, count, position, group, or object associated with that key.
Typical lookup, insertion, and update are treated as O(1) average time.
Python Example
counts = {}
for word in ["red", "blue", "red"]:
counts[word] = counts.get(word, 0) + 1After the loop, counts["red"] is 2 and counts["blue"] is 1.
Common Confusions
The key must be hashable. Strings, numbers, and tuples of hashable values can be keys. Lists cannot be keys because they are mutable.
Hash maps do not keep values sorted. If the algorithm needs sorted order, a plain Python dictionary is not the right structure by itself.
When To Use It
Use a hash map when the algorithm needs fast lookup by key, counting frequencies, grouping items, storing previously seen positions, or memoizing results.