#K94802. Find the Odd Occurrence
Find the Odd Occurrence
Find the Odd Occurrence
You are given an array of n integers. In the array, exactly one integer appears an odd number of times, while all other integers appear an even number of times. Your task is to find and output the integer that occurs an odd number of times. If all numbers appear an even number of times, output 0.
This problem can be efficiently solved using the XOR operation because the XOR of a number with itself equals 0 and the XOR operation is both commutative and associative.
Mathematically, if the array is \(a_1, a_2, \dots, a_n\), compute:
\[ \text{result} = a_1 \oplus a_2 \oplus \dots \oplus a_n \]The final result will be the number that appears an odd number of times, or 0 if none exists.
inputFormat
The first line of input contains an integer (n), which is the number of elements in the array. The second line contains (n) space-separated integers.
outputFormat
Output a single integer: the number that appears an odd number of times. If no number appears an odd number of times, output 0.## sample
9
2 3 5 4 5 3 4 2 4
4