Coding & data structures · Hashing, counting, and prefix sums

Counting: frequency as state you can update

Why a frequency map beats sorting for almost every "compare these multisets" question, when to count into an array instead, how a count becomes incremental state that composes with a sliding window, and the two O(n) tricks — bucket-by-count and the majority vote — that a heap-shaped answer misses.

22 min read Full lesson in the app Patterns: counting, frequency map, bucket sort by count, majority vote

What you'll be able to do

Before this: hash-maps, sliding-window

The rest of this lesson is in the app

Why a frequency map beats sorting for almost every "compare these multisets" question, when to count into an array instead, how a count becomes incremental state that composes with a sliding window, and the two O(n) tricks — bucket-by-count and the majority vote — that a heap-shaped answer misses. This walkthrough runs about 22 minutes, with runnable code you can edit and re-run as you read.

Continue in ChannelPulse

The first module of every track is free to read on the web — see what's open in Coding & data structures.