A gym's front desk hands out a numbered locker tag to every member who checks in. The tags are reused across the club's history, so on a busy day the same tag number is sometimes handed to more than one member by mistake, creating a scheduling conflict for that tag. At closing time the manager wants to know the largest tag number that was handed out to exactly one member that day — a tag with no conflict — so that member can be given priority to keep using that locker tomorrow. If every tag number that appears was handed out more than once (or no check-ins happened at all), there is no such tag.
Line 1: an integer n — the number of check-ins that day (0 <= n <= 100000).
Line 2 (present only when n > 0): n space-separated integers, the locker tag number handed out at each check-in, in the order the check-ins happened. A tag value may repeat.
Print a single integer: the largest tag number that appears exactly once among the n check-ins. If no tag number appears exactly once (this includes the case n = 0), print -1.
Example 1
Input
6 5 7 3 9 3 7
Expected
9
Explanation
Tag counts are 5:1, 7:2, 3:2, 9:1. The tags that appear exactly once are 5 and 9; the largest of these is 9, so the answer is 9.
Example 2
Input
4 2 2 4 4
Expected
-1
Explanation
Both tags 2 and 4 were each handed out twice, so no tag appears exactly once. The answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →