1. Concept library
  2. Data Structures & Algorithms
  3. Module 3: Hash maps and sets

Concept

Frequency counting

Count how often each value appears with a hash map, then answer questions from the counts: anagrams, most common element, first unique character. It turns many O(n^2) comparisons into O(n).

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how a hash map of counts turns a comparison problem into one pass
  2. Solve an anagram or first-unique-character problem with a counter
  3. Spot the O(n log n) sort used where a count is O(n)

In practice

A few lines that show Frequency counting doing its job.

Python
from collections import Counter

def is_anagram(a, b):
    return Counter(a) == Counter(b)

def first_unique(s):
    counts = Counter(s)
    for i, ch in enumerate(s):
        if counts[ch] == 1:
            return i
    return -1

The common mistake

Watch for

Sorting both strings to compare anagrams when a count works. Sorting is O(n log n) and allocates; a counter is O(n) and reads as what it means.

What to know first

Your roadmap puts these before Frequency counting, unless you already know them.

How it stays learned

Once a session shows you understand Frequency counting, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Frequency counting.

Previous in the module← Hashing and collisions
Next in the moduleLookup trade-offs →