← All patterns
27 / 32
Hashing & Sets

Hash Map Counting

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

frequencycountanagramtwo sumseen beforelookup
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Do you need to remember whether a value appeared before?
  2. Are counts, frequencies, or complements central to the answer?
  3. Can one pass with a Map replace a nested scan?

Classic problem

Two Sum (LeetCode 1) — return the indices of the two numbers in nums that add up to target. Store each value as you go and look up its complement.

Input: nums = [2, 3, 5, 7, 4], target = 11

Output: [3, 4]nums[3] + nums[4] = 7 + 4 = 11, complement 7 found in the map on the 5th iteration.

Watch it run

Common pitfalls

Forgetting that 0 is a valid stored count or index.

Using an object when keys may not be safe property names.