Hashing and frequency
Use a hash set or map as a memory of what you have seen, so "have I met the partner of this value?" or "how many copies are left?" is answered in one look-up instead of another scan.
How to recognise it
- A pair search, such as two values adding up to a target, where each element looks for a partner.
- Questions about membership (is it present?), duplicates or "first repeated / first unique".
- Counting with multiplicity, such as "is every element of B available in A, counting repeats?".
- The values are unsorted and sorting would cost O(n log n) or destroy the original positions you need.
The idea in one picture
Think of a cloakroom attendant. Instead of searching every coat when someone hands in a ticket, they look up the ticket number on a board and walk straight to the right hook. A hash set or map is that board: it answers "do I have this?" or "how many of these do I have?" in about one step, whatever the size of the cloakroom.
Hashing basics used counts to answer questions afterwards. Here the map is consulted during the pass, as a memory of everything seen so far.
Four uses
| Use | Store | Example in this pattern |
|---|---|---|
| Find a partner | value → index (or count) | Two Sum, pairs with a given sum |
| Membership | set of values | Longest consecutive sequence, first repeated word |
| Copies left | value → count remaining | Is one array a subset of another |
| State per item | item → small state | Customers and computers, first unique in a stream |
The partner template
seen = empty map
for i, x in nums:
if (target - x) is in seen: # look back for a partner
use seen[target - x] and i
record x in seen # only after looking, so x never pairs with itself
This one shape turns many O(n²) pair searches into O(n).
A guided example: which two indexes in [2, 7, 11, 15] add up to 9?
| i | x | need (9 - x) | in seen? | seen afterwards |
|---|---|---|---|---|
| 0 | 2 | 7 | no | 2 → 0 |
| 1 | 7 | 2 | yes, at 0 |
The answer is [0, 1], found after looking at two elements.
When not to use hashing
- When the values are small and non-negative, an array indexed by value is simpler and faster.
- When order matters (smallest, next bigger), a sorted structure or sorting is the right tool.
- When the problem asks for O(1) extra memory, hashing is usually not allowed; sorting with two pointers often is.
Common mistakes
- Adding the current value before looking up its partner, which pairs a value with itself.
- Using a set where repeats matter, such as counting pairs or checking a subset with duplicates.
- In C++, reading
map[key]to check for a key, which silently inserts it. - Assuming a hash map keeps insertion order. It does not in Java (
HashMap) or C++ (unordered_map).
Problems
- Two Sumeasyguide ready
- Longest Consecutive Sequencemediumguide ready
- find all pairs on integer array whose sum is equal to given numbermediumguide ready
- Find whether an array is a subset of another arraymediumguide ready
- Common elements in all rows of a given matrixmediumguide ready
- Find the first repeated word in stringmediumguide ready
- Function to find Number of customers who could not get a computermediumguide ready
- Find the first non-repeating character from a stream of charactersmediumguide ready