Skip to content
Hashing & FrequencyBeginner

Hash Map Counting

Use a hash map to count frequencies or track seen elements for O(1) lookups.

Problem: First Unique Character in a String

Find the first non-repeating character in "leetcode" and return its index.

Frequency Map
'l': 1

Count char 'l' at index 0

Start building frequency map. First character 'l' appears once so far.

Step 1/10
Speed:

When to Use

  • Count element frequencies
  • Check if element exists
  • Find duplicates
  • Group anagrams

Key Indicators

  • Frequency counting
  • "Find duplicates"
  • Group by property
  • O(1) lookup needed

Complexity

Time

O(n)

Space

O(n)

Common Problems

First Unique Character in a StringGroup AnagramsTop K Frequent ElementsValid AnagramContains Duplicate

Code

freq = {}
for element in arr:
    freq[element] = freq.get(element, 0) + 1
# Now freq[x] gives count of x in O(1)

Want all 29 patterns with detailed visual walkthroughs?

The complete book includes 84 practice problems, full decision tree, and illustrated step-by-step solutions.

Book Coming Soon ($4.99)