write a method to return the duplicate in a integer array
Anonym
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;