#C818. Find the Odd Occurrence
Find the Odd Occurrence
Find the Odd Occurrence
Given an array of integers where every element appears an even number of times except one, your task is to find that single integer that appears an odd number of times.
The solution leverages the properties of the XOR operation, where for an array \(a_1, a_2, \dots, a_N\), the following holds:
$$result = a_1 \oplus a_2 \oplus \cdots \oplus a_N$$
Because XOR-ing a number with itself yields 0 and XOR is commutative and associative, all elements occurring even times cancel out, leaving the odd-occurring element as the result.
inputFormat
The first line contains an integer (N) representing the number of elements in the list. The second line contains (N) space-separated integers.
outputFormat
Output a single integer – the number that appears an odd number of times.## sample
5
2 2 3 2 2
3