Double-Scanned Tickets

Easyhash mapAlso known as Contains Duplicate

You run the door at a small concert. The scanner logs each ticket code it reads, in the order people walk in. Each ticket is good for one entry, so a code that shows up twice means someone copied a ticket. After the doors close, you want to know whether that happened tonight.

Your task

Write hasRepeat(codes). Return true if any code appears in the log two or more times, and false if every code is different.

An empty log has no repeats, so return false.

hasRepeat(codes: int[]) → bool

Examples

Example 1
Input
codes = [4021, 7788, 1290, 7788, 5003]
Output
true
Why

Code 7788 was scanned at positions 1 and 3. One repeat is enough, so the answer is true.

Example 2
Input
codes = [310, 52, 9001, 47]
Output
false
Why

Four scans, four different codes. Nobody got in twice.

Example 3
Input
codes = [88, 15, 42, 88]
Output
true
Why

The repeat need not be next to the original: here it came in three people later.

Constraints

  • 0 ≤ codes.length ≤ 100,000
  • 0 ≤ codes[i] ≤ 1,000,000,000
Start with brute force

Comparing every scan with every later scan takes about n²/2 comparisons, around 5 billion for 100,000 scans. Sorting and comparing neighbours is O(n log n), but still does more work than needed and reorders the log.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code