Home › Learn › World 3: Hash Harbor
FREEDuplicate 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
| Time | O(n) |
| Space | O(n) |
How the stars are earned
- Correct verdict on every test
- At most n² reads
- 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.
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
- Locker Room: Hash map put and get, O(1) each
- Frequency Board: Counting with a hash map, O(n)
- Two Sum Harbor: Hash map complement lookup, O(n)
- Anagram Crates: Letter counts in a hash map, O(n)
- Longest Chain: Hash set sequence starts, O(n)
- Smuggler Twins: Prefix sums + hash map, O(n)