Double-Scanned Tickets

Easyhash mapAlso known as Contains Duplicate

You run the door at a small concert. Every ticket has a printed code, and the scanner writes each code it reads into a log, in the order people walk in.

Each ticket is good for one entry. If the same code shows up twice, someone copied a ticket. After the doors close, you want to know: did that happen 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 does not have to be next to the first scan. Here the copied ticket came in last, three people after the original.

Constraints

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

Compare every scan with every later scan, and return true as soon as two match.

That is about n²/2 comparisons. With 100,000 scans it is around 5 billion checks, far too many. Sorting first and comparing neighbours is faster, O(n log n), but it 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