Skip to content
CodeItRaw
Hashing

Lesson 4/5

Some questions can only be answered after seeing the whole list: "which is the first element that appears only once?" Looking at the first element you cannot know whether it repeats later.

count = {}
for v in values:
count[v] = count.get(v, 0) + 1
for i, v in enumerate(values):
if count[v] == 1:
return i
return -1
The first pass counts; the second walks the list again in its own order and asks the counts. Two passes are still 2n reads, not n × n.

It matters that the second pass runs over the list and not over the dictionary: the word "first" means the order in the list.

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

    The Odd One

    Function

  2. 02

    Ticket Queue

    Function

  3. 03

    Common Items

    Code reading · Read the Big-O

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