**Problem**

In a given array of integers, each integer repeats itself except for one. Find that unpaired integer.

**Example**

[1,2,2,1,3] => 3

**Solution**

result = 0 for i in arr result ^= i end return result

**Explanation**

XOR operation is associative and commutative, so…

Given our solution, [1,2,2,1,3] will give

= 1 ^ 2 ^ 2 ^ 1 ^ 3

= 1 ^ 1 ^ 2 ^ 2 ^ 3

= (1 ^ 1) ^ (2 ^ 2) ^ 3

= 0 ^ 0 ^ 3

= 0 ^ 3

= 3

Advertisements