Help understanding java algorithm using arraycopy to remove duplicate elements
Im having difficulty understanding how the array is being copied in this algorithm. Im am fine with the code till i get to line 12 shown below. This code was from a book Im currently studying. Im just trying to understand how the copy is carried out on each match
Wrote some values to simulate the a scenario
a = [10,4,6,10,5,2]
a[0] = a[3] MATCH occurs when j=3
arraycopy(a, 4, a, 3, (5-3)) // Variable values substituted into arraycopy. (pretty sure they're correct)
System.arraycopy(a, j+1, a, 0, n-j); // Line 12
The whole code:
int n = a.length;
if (n < 2){
System.out.println("no duplicates");
}
for (int i = 0; i< n-1; i++)
for (int j = i + 1; j < n; j++ )
if(a[i] == a[j]){
--n;
System.arraycopy(a, j+1, a, 0, n-j); // Line 12
--j;
System.out.println();
}
int[] aa = new int[n];
System开发者_运维知识库.arraycopy(a, 0, aa, 0, n);
Just for the record I understand the second arraycopy statement since its just a straightforward copy into array aa.
I know there are many arraycopy questions on SO but the ones i encountered did not answer this specific question because i need an step-through understanding of how the copying occurs match by match.
Anyway thanks in advance guys.
I'm pretty sure there's a bug in the algorithm, since the first array copy will overwrite good data at the front of the array. I think the line should be:
System.arraycopy(a, j+1, a, j, n-j); // Line 12
This would shift the end of the array over one, overwriting only the one duplicate that was just found.
Right now, it works like this:
Before copy:
i j n
[1, 2, 3, 4, 1, 5, 6]
\ /
n-j
After copy:
i j n
[5, 6, 3, 4, 1, 5, 6]
Clearly wrong. There are now more duplicates than when we started!
With the updated line, it looks like this:
After copy:
i j n
[1, 2, 3, 4, 5, 6, 6]
Much better. The duplicate 1 is gone.
As an additional note, this is a pretty horrible way to eliminate duplicates from an array. An array that was all duplicates would perform (n * (n - 1) / 2) element copies, which is ludicrous. The standard simple way to dedupe an array is usually to add each element to a HashSet
, then iterate that set's contents into a new array.
精彩评论