I created a method which search for duplicates and then stores duplicates index into another array. Then I’m running through my big array and move all entries without duplicates.
Now, my problem is that this uses O(N*N) and I am using additional memory space since I am adding additional array.
How could this be done? Assuming that I need to understand how this could be done without using additional libraries or HashSet.
Any tips appreciated.
public void dups()
{
int[] index = new int[100];
int k = 0;
int n = 0;
int p = 0;
for (int i = 0; i < elements; i++)
for (int j = i + 1; j < elements; j++)
if(a[j].equals(a[i]))
index[k++] = i;
for (int m = 0; m < elements; m++)
if (m != index[p])
a[n++] = (T) a[m];
else
p++;
elements -= k;
}
You can’t find duplicates in
O(n)(in general).However it is possible in
O(n*log n). Simply sort your array (O(n*log n)), and then the scanning for duplicates can be done inO(n).On the other hand, if you can use hash tables (what you probably don’t want to do, if you don’t want to use any additional libraries) you can scan through the array and count how often each element appears in the array. After that, you can go through each element in the hash table and find those elements that appeared more than once. This would take an expected runtime of
O(n), but not deterministicO(n).Finally, why did I write that you can’t find duplicates in
O(n)in general?One could imagine several special cases, where finding duplicates is possible in
O(n).For example, your array could only contain numbers from 0 to 99.
In that case, you could use another array (of size 100) to count how often each element appears in the array. This works the same way as with the hash table, but its runtime would be deterministic
O(n).Another example where finding duplicates is possible in
O(n)is of course, if the array is already sorted.