Hash Map / Set
Hash maps and sets trade memory for O(1) average lookups, insertions, and membership tests. Reach for one whenever you catch yourself asking 'have I seen this before?' or 'how many times?' — they turn nested-loop searches into a single pass.
22 LeetCode problems solved with the Hash Map / Set pattern. Practice them with spaced repetition so the pattern sticks.
- 4Sum IIMEDIUM · O(n^2)
- Contains DuplicateEASY · O(n)
- Contains Duplicate IIEASY · O(n)
- Count Good MealsMEDIUM · O(n * log maxSum)
- Determine if Two Strings Are CloseMEDIUM · O(n + k log k)
- Equal Row and Column PairsMEDIUM · O(n^2)
- Find the Difference of Two ArraysEASY · O(n + m)
- First Missing PositiveHARD · O(n)
- Group AnagramsMEDIUM · O(n*k log k)
- Intersection of Two ArraysEASY · O(n + m)
- Isomorphic StringsEASY · O(n)
- Longest Consecutive SequenceMEDIUM · O(n)
- Longest PalindromeEASY · O(n)
- Majority Element IIMEDIUM · O(n)
- Max Number of K-Sum PairsMEDIUM · O(n)
- Max Points on a LineHARD · O(n^2)
- Ransom NoteEASY · O(m + n)
- Two SumEASY · O(n)
- Unique Number of OccurrencesEASY · O(n)
- Valid AnagramEASY · O(n)
- Valid SudokuMEDIUM · O(1)
- Word PatternEASY · O(n + m)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →