Amazon interview question

There are 199 elements in array. It has only one distinct value? How will find this distinct value? Also for solution please tell about O(n)?How can you make it more efficient?

Interview Answers

Anonymous

30 Aug 2011

Above answer won't work on a data set like: {3, 3, 3, 2, 2, 12} where there's an odd number of non-unique values. The easy answer is that you could hash each element into a hash table which just contains a counter for each key add. Then you could go through the array again and look at the hash table to find the element that was only added once.

2

Anonymous

27 Oct 2011

... for(int i=0 ; i entity : map.entrySet()){ if (entity.getValue() == 1){ System.out.println("distinct element is: " + entity.getKey()); break; } }

Anonymous

13 Jun 2011

XOR all the elements. 198 are the same. The result will be 0 for them: A XOR A = 0. The result will be the distinct value.

Anonymous

13 Jun 2011

Forgot to indicate: 0 XOR B = B. Therefore, the overall result will be the distinct B.