๐ 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:
- Base case โ an input answered directly, without recursing.
- 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 CodeAlgorithm 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