Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Frequency maps and recursion

โญ beginnerโณ 13 min read๐Ÿ“ Lesson 80 of 85

The dictionary-as-counter pattern solves a family of problems, and recursion is just a contract: base case plus smaller step.

The frequency map

Counting things is the most reusable loop shape in the collections chapter:

var counts = new Dictionary<string, int>();
foreach (string w in words)
    counts[w] = counts.GetValueOrDefault(w) + 1;

Many problems are this pattern wearing a costume: first duplicate, top word, anagram check, pair-sum lookup โ€” each is a dictionary doing the memory work that a nested loop would redo O(nยฒ) times.

The recursion contract

Every recursive method needs two things, or it never terminates:

  1. Base case โ€” an input answered directly, without recursing.
  2. Smaller step โ€” each call works on a strictly smaller input, moving toward the base case.

The call stack does the bookkeeping: every call gets its own frame with its own locals, and the answer is assembled as frames return.

โšก Now practice

Ready to Code
Algorithm workshopDuplicates, binary search, tie-breaking, bracket matching โ€” each a pattern from the lessons, each with edge cases that punish near-misses.
4 challenges ยท ยท ~45 min