Pergunta de entrevista da empresa Amazon

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?

Respostas da entrevista

Sigiloso

30 de ago. de 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

Sigiloso

27 de out. de 2011

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

Sigiloso

13 de jun. de 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.

Sigiloso

13 de jun. de 2011

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