First rescan: hints, no solution

Easy · Arrays & hashing

A receiving dock scans every pallet id as the pallet passes the gate, and the scanner appends each id to a log in the order it was read. A pallet that goes through twice usually means a mis-route, so the shift lead wants the first repeat caught.

Given the log, walk it from the start and return the id of the first entry that has already appeared earlier in the log. In other words, find the pallet whose second scan comes soonest. If no id is scanned twice, return -1. Pallet ids are positive integers.

Examples

Input
scans = [4, 9, 2, 9, 4]
Output
9
Note
The second 9 at position 3 arrives before the second 4 at position 4.
Input
scans = [1, 2, 3]
Output
-1
Note
No id repeats, so the answer is -1.
Input
scans = [7, 7]
Output
7
Note
The very first repeat is the answer even when it is adjacent.

Constraints

  • 0 <= scans.length <= 140000
  • 1 <= scans[i] <= 1000000
  • Return -1 when no id repeats

Hint 1: a nudge

When you reach an entry, what one question about the entries before it decides whether it is the answer?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first