A sensor logs integer readings into a buffer. By design, every distinct reading value is recorded an even number of times — except for exactly one "lonely" value that was recorded an odd number of times. Given the buffer, recover that lonely value.
The input guarantees that exactly one value has an odd count, so the answer is unique.
Line 1: an integer n, the number of readings.
Line 2: n space-separated non-negative integers, the readings.
A single integer: the value whose count in the buffer is odd.
Example 1
Input
7 4 1 2 1 2 4 3
Expected
3
Explanation
4, 1, and 2 each appear twice (even), while 3 appears once (odd). XOR-ing all values cancels the paired ones and leaves 3.
Example 2
Input
5 0 5 5 7 7
Expected
0
Explanation
5 and 7 each appear twice and cancel out; 0 appears once, so the lonely value is 0.
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 →