Interview patterns

Hash Map Counting Pattern Explained for Coding Interviews

Updated Nov 27, 2026 · 3 min read

Walk the input once and keep a count of each item in a hash map, like a Python dict or collections.Counter. Then answer the question from the map: the most common item, the first unique one, or whether two words have the same letters. It takes O(n) time and O(k) space for k distinct items, instead of comparing every pair in O(n²).

Watch onYouTubeInstagram

Key points

  • ●Clue words: count, frequency, most common, at least k times, same letters, first unique, majority.
  • ●Build the map in one pass: count[x] = count.get(x, 0) + 1.
  • ●Two words are anagrams when their frequency maps are equal.
  • ●O(n) time and O(k) space, versus O(n²) for comparing every pair.
Step-by-step visual sheet: Hash Map Counting
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: 15 pairs or 6 steps

Which letter shows up most in "banana"?

You could compare every letter with every other letter. Six letters make 15 pairs. Or you could walk the word once, keep a tally, and be done in 6 steps. That tally is a hash map, and the pattern is called hash map counting, or a frequency map.

How to spot it

The problem tells you, if you know the words. Reach for a frequency map when it says:

  • count, frequency, how many times, most common or at least k times
  • same letters: anagrams, or whether word B can be built from word A
  • first unique, duplicate or majority element

All of these are questions about how often things appear. Counting once answers them all, so you build the map first and only then decide what to ask it.

The idea: count once, answer instantly

Walk the input one item at a time. For each item, add one to its count in a dict. When you've seen everything, the map holds every answer you need, and looking anything up in it is instant.

The quick recall on the sheet is three steps: spot the clue word, build the map in one pass, then run the query that matches the task. Most common means the biggest count. First unique means walking the input again and stopping at the first item with a count of 1. Anagram means comparing two maps.

Trace it on "banana"

Step Letter Map after the step
1 b b 1
2 a b 1, a 1
3 n b 1, a 1, n 1
4 a b 1, a 2, n 1
5 n b 1, a 2, n 2
6 a b 1, a 3, n 2

Now read off the answers:

  • Most common: a, 3 times.
  • First unique: b, the first letter with a count of 1, at index 0.

The same works for anagrams. listen and silent both give l1, i1, s1, t1, e1, n1. Equal maps, so they're anagrams. The visual sheet above shows the two maps next to each other.

The Python template

count = {}
for x in items:
    count[x] = count.get(x, 0) + 1

count.get(x, 0) returns the current count, or 0 if x hasn't been seen yet, so the first time you see an item it becomes 1.

Python also has a shortcut:

from collections import Counter
count = Counter(items)

With it, max(count, key=count.get) on "banana" gives "a", and Counter("listen") == Counter("silent") is True.

Complexity

  • Hash map counting: O(n) time, O(k) space for k distinct items.
  • Compare every pair: O(n²) time.

For "banana" that's one pass over 6 letters against 15 pairs. The gap grows fast: the pair count goes up with the square of the length.

Where it shows up

Practice it on LeetCode 242 Valid Anagram, 387 First Unique Character in a String, 169 Majority Element, 347 Top K Frequent Elements and 451 Sort Characters By Frequency. The counting part is the same in all five; only the question you ask the map changes.

The trap is skipping the map and comparing items pair by pair because it feels simpler. It works on "banana". It doesn't work on a million items. The other trap is forgetting the space: the map holds one entry per distinct item, so say O(k) space when an interviewer asks.

Frequently asked questions

When should you use a hash map to count?

When the problem says count, frequency, how many times, most common or at least k times; when you compare contents, like anagrams or whether word B can be built from word A; or when you need the first unique item, a duplicate or a majority element.

How do you count frequencies in Python?

Loop over the items with count[x] = count.get(x, 0) + 1 on a plain dict, or use the shortcut count = Counter(items) from the collections module.

How do you check if two strings are anagrams with a hash map?

Build a frequency map for each string and compare them. Counter('listen') == Counter('silent') is True, because both give l1, i1, s1, t1, e1, n1.

What is the time and space complexity of hash map counting?

O(n) time for one pass over n items, and O(k) space, where k is the number of distinct items. Comparing every pair is O(n²).

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free