#K71187. Find the Unique Element
Find the Unique Element
Find the Unique Element
You are given an array of integers where every element appears exactly twice except for one element that appears only once. Your task is to find and output that unique element.
If every element in the array appears twice, the result will be 0. This solution makes use of the bitwise XOR operation, which has the property that \(a \oplus a = 0\) and \(a \oplus 0 = a\). Thus, when all duplicate elements cancel each other out, only the unique element remains.
For example:
- Input: 2 3 5 4 5 3 2, the unique element is 4.
- Input: 1 2 4 2 1, the unique element is 4.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains an integer n, representing the number of elements in the array.
- The second line contains n space-separated integers.
outputFormat
Output the unique element, which is the number that appears only once. If all elements appear twice, output 0.
## sample7
2 3 5 4 5 3 2
4
</p>