Skip to content
CodeItRaw
Hashing

Lesson 1/5

Ask a list "is this value in you?" and it looks through its elements one by one to answer. A set answers the same question in one step, however many elements it holds.

seen = set()
for v in values:
if v in seen:
return True
seen.add(v)
return False
At each element ask first ("have I seen this before?"), then add. A set holds each value only once; adding the same value twice changes nothing.

The price is memory: you keep what you have seen somewhere. In return two nested loops become one. That trade (give a little memory, gain a lot of time) is the whole of this topic.

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

    Any Repeats?

    Function

  2. 02

    Any Duplicates?

    Code reading · Review the AI

  3. 03

    Any Repeat?

    Code reading · Break the Code

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