Learn / Arrays

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

UseStoreExample in this pattern
Find a partnervalue → index (or count)Two Sum, pairs with a given sum
Membershipset of valuesLongest consecutive sequence, first repeated word
Copies leftvalue → count remainingIs one array a subset of another
State per itemitem → small stateCustomers 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?

ixneed (9 - x)in seen?seen afterwards
027no2 → 0
172yes, 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

0 of 8 solved

Check yourself

In Two Sum, why look up the partner before adding the current value to the map?

For "longest run of consecutive values", why only start counting from x when x - 1 is not in the set?

To check that every value of b appears in a, counting repeats, what should the map store?

You must output, after each new character of a stream, the first character so far that has appeared exactly once. Which pair of structures fits?