progress

~/problems / Arrays & hashing / Hash maps and counting

Contains Duplicate

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~10 min

A stadium gate logs the ID of every ticket it scans. A ticket is valid only once, so a repeated ID means someone got in on a copy.

Write scanned_twice(tickets) -> bool that returns True if any ID appears more than once in tickets, and False if every ID is different.

scanned_twice([804, 17, 3302, 17, 55])   # True   (17 appears twice)
scanned_twice([9, 1, 42])                # False
scanned_twice([])                        # False

Constraints: 0 <= len(tickets) <= 2 * 10^5; IDs are integers in [0, 10^12].

⭐ Bonus: a speed test with 200,000 scans earns a star if you do it in one pass (O(n)); comparing every pair is O(n²) and still passes.

Show hint

Walk the log once and remember every ID you've already met, in a structure that answers "have I seen this before?" in O(1).

Topic: Hash maps and counting. Counter/defaultdict, complement lookups, prefix-sum + hash map.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc