How do you remove duplicates in a large array.
Anonymous
Create a hash table with size equal to n, the number of items in the array. Set a counter, uniqueCount to 0. Then walk through the array. Check if a[i] is in the hash table. If it is not, add a[i] to the hash table with the unique count as its value. Increment the unique count. At the end, put the hash results into your array, using each key’s value as its index, and set all remaining elements to null. The worst-case for this solution is O(n), but it also requires O(n) memory to store the hash table. Is there a way to do this in place? removeDupes(int[] a){ int uniqueCount = 0; for (int i = 0; i < n; i++){ if (a[i] not in hash table) { add a[i] to hash table with value uniqueCount; uniqueCount++; } } set = get key-value pairs from hash table; for (each element e in set) { a[e.value] = e; } for (int i = uniqueCount; i < n; i++){ a[i] = null; } }
Check out your Company Bowl for anonymous work chats.