Skip to content
CodeItRaw
Hashing

Lesson 3/5

A dictionary puts a value next to each key: {"a": 3, "b": 1}. A set answers "is it there?"; a dictionary answers "how many?" or "where?".

count = {}
for v in values:
count[v] = count.get(v, 0) + 1
count.get(v, 0) gives v's count if it was seen before, and 0 if not. So the first meeting writes 1 without an error.

Once the counting is done you can walk the dictionary: for value, n in count.items(). Finding the most frequent is the "best so far" pattern you know, run over a dictionary.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.

  1. 01

    Letter Count

    Code reading · Predict the Output

  2. 02

    Most Frequent

    Function

  3. 03

    Most Common Word

    Code reading · Bug Hunt

If you would rather not wait for the order, every problem is open without locks: Problem list