Possible Duplicate:
Find integer not occurring twice in an array
Accenture interview question – find the only unpaired element in the array
Given an array of integers of odd size. All the integers in the array appear twice except for a single integer. How to find this uncoupled integer in most efficient (both memory and complexity-wise) way?
If you XOR all of them together, you’ll end up with the lone (uncoupled) value.
That’s because
xXORxis zero for allxvalues, and0XORxisx.By way of example, the following program outputs
99:In terms of efficiency, it’s basically O(1) space and O(n) time complexity, minimal, average and worst case.