0

Frequency maps

Patternsused in ~100% of apps

The most reused four lines in the whole course. Turn a list into a tally of how often each thing appears, and a surprising number of hard-looking questions become easy questions about a small table.

Core idea. Walk the data once, building a Map from each value to how many times it appeared. Then work on the tally instead of the original list. The tally is usually far smaller, and questions about counts become direct lookups.
Playground, this isn't a screenshot
["a", "b", "a", "c", "a", "b"] → one pass → a tally
6 items
a
b
a
c
a
b
→
3 rows
a→3
b→2
c→1

Six items became three rows. "Which appears most?", "does anything appear exactly twice?", "are these two lists the same bag of items?". All now questions about a three-row table.

↑ play a little first, then dig in