CODE THE HERO

Home › Learn › World 3: Hash Harbor

FREE

Duplicate Parcel

Hash set membership, O(n)

Level 3.2 of Code the Hero, in Hash Harbor: hash maps: find anything in one visit.

The idea

A hash set answers 'have I seen this?' in O(1), turning the O(n²) pair check into one pass.

The mission

Smugglers slipped a copy into the delivery! Is any parcel number here twice? Comparing every pair is slow. Instead, remember each number in the lockers as you go. Before storing a number, check if its locker already has it.

Goal: submit(true) if any parcel number appears twice, otherwise submit(false).

The commands you get

You write real Python, Java or JavaScript. These are the moves the hero understands in this level:

parcels.length / parcels.get(i)Read parcel i's number
lockers.has(key)Is anything stored under key? (1 visit)
lockers.put(key, value)Store something under key
submit(bool)Hand in your verdict

What it costs

TimeO(n)
SpaceO(n)

How the stars are earned

  1. Correct verdict on every test
  2. At most n² reads
  3. At most n reads: one pass

Your code is also checked on hidden inputs, so it has to work for any case, not only the one on screen.

▶ Play this level free

No sign-up. Levels open in order, so if this one is still locked you start at the first level that isn't.

More in Hash Harbor