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
- Do you need to remember whether a value appeared before?
- Are counts, frequencies, or complements central to the answer?
- Can one pass with a
Mapreplace 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.
Common pitfalls
Forgetting that
0is a valid stored count or index.
Using an object when keys may not be safe property names.