Hash map
Applies to: general, python, cpp
A hash map stores key-value pairs and finds a key in O(1) average time by hashing it to a bucket. It is the
go-to structure for lookups, counting, and de-duplication. Python's dict and C++'s std::unordered_map are hash
maps.
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1 # count occurrences