Count in a map
transcript
A hundred thousand words. Which one is most common? Count in a map.
Scanning for each word is slow. Ten billion checks. About a hundred seconds. A map is a table of counts. Use it for any counting job. Words, ids, or error codes. A million words still fit.
A map reads the list once. About a hundred thousand steps. A thousandth of a second. Each word costs one lookup. Try it on a small list first. One pass is enough. That is a million steps.
Look up the word. Add one to its count. Move on to the next word. No repeated scans. Then trust it at scale. That is the whole trick. The scan would need a trillion.
The highest count wins. On a tie, take the smaller word. It is correct and fast. The same idea counts anything. It fits the limit easily. Pick the tool by its cost.
A map reads the list once. Full breakdown in the lecture.