Pergunta de entrevista da empresa Microsoft

write a method to return the duplicate in a integer array

Respostas da entrevista

Sigiloso

30 de ago. de 2017

You would always return a false positive in your double for loop because you'd compare the first element with itself and suggest its a duplicate. Using a boolean array could work, but depending on how you set it, it may be wasting space. The dictionary is a better solution, but it still is wasting space. There is no need to keep keys and values in the instance. The best solution is to use a set to keep track of everything you've seen thus far. Example: Set found = new HashSet(); for (int i = 0; i < array.length; i++) { if (found.contains(array[i]) return array[i]; found.add(array[i]); } // denotes no duplicates return -1;

Sigiloso

18 de ago. de 2017

This doesn't seem right to me. This solution is just telling you if the item at position i is equal to item at position j which is always true. My thinking is to add each value you find to a dictionary and when going to the next one, check if it exists. If it does then it is a duplicate so return that.

Sigiloso

19 de jul. de 2017

int dupElement(int[] array){ for(int i=0; i < array.length;i++){ for(int j=0; j < array.length; j++){ if(array[i] == array[j]) return array[i]; } } } Gave another solution by adding boolean array which can return value with order of O(n) and O(1)

1