#C10682. Unique Number Finder

    ID: 39914 Type: Default 1000ms 256MiB

Unique Number Finder

Unique Number Finder

You are given a list of n integers. In this list, every integer appears exactly twice except for one integer that appears only once. Your task is to find and output the unique integer. If there is no such unique integer (i.e. all elements appear in pairs), output 0.

The solution can be achieved efficiently using the bitwise XOR operator. Recall that the XOR operator has the following properties:

  • Commutative and Associative: \(a \oplus b = b \oplus a\) and \(a \oplus (b \oplus c) = (a \oplus b) \oplus c\).
  • Self-cancellation: \(a \oplus a = 0\) for any integer \(a\).
  • Identity: \(a \oplus 0 = a\).

Using these properties, if you XOR all numbers in the array, the duplicate numbers cancel each other, leaving the unique number (or 0 if every number is paired).

inputFormat

The first line of input contains a single integer \(n\) (\(1 \leq n \leq 10^5\)), representing the number of elements in the list. The second line contains \(n\) space-separated integers, each of which can be positive, negative, or zero.

outputFormat

Output a single integer which is the unique number that appears only once. If there is no such number, output 0.

## sample
5
2 3 5 2 5
3