Some solutions are:
- Making use of hash, but that would result in linear time complexity but very poor space complexity
- Sorting the list using MergeSort
O(nlogn)and then finding the element which doesn't repeat
Use the basic idea of XOR to solve this problem.
"one element appears once, all others an even number of times", then the solution would be to XOR the elements. The reason is that
x^x = 0,
so all the paired elements would vanish leaving only the lonely element.
Basically, it makes use of the fact that
x^x = 0 and
0^x=x. So all paired elements get XOR'd and vanish leaving the lonely element.