Amazon Interview Question

How to find the unique integer in an array, where all the other numbers appear twice.

Interview Answers

Anonymous

Mar 25, 2010

XOR all the elements into a variable and that variable will have the unique integer.

2

Anonymous

Apr 17, 2010

XOR is the right answer because all the other numbers occur only twice. So for {3,3,1} the answer is 1.

1

Anonymous

Apr 11, 2010

XOR will not work ... consider that u have array of 4 elements {3,3,3,1}. XORing them all gives u 2 which is incorrect.

1