Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this code remove duplicates from a sorted array?

Tags:

java

arrays

If I pass in a number, an array 2, 3, 3, 3, 1, for example. It removes the duplicates of 3, but why? and the result is 123 (which it should, because of the sorting method).

Sortering.sorterHeltallstabell(tab) is only sorting my code, while the rest removes the duplicates. It gets sorted before duplicates are removed.

Why does this code remove duplicates of an array when you pass it into the method?

public static int[] utenDubletter(int[] tab){
    Sortering.sorterHeltallstabell(tab);

    if (tab.length < 2)
        return tab;

    //why does this code remove duplicates?
    int j = 0;
    int i = 1;

    while (i < tab.length) {
        if (tab[i] == tab[j]) {
            i++;
        } else {
            j++;
            tab[j] = tab[i];
            i++;
        }           
    }              
    int[] B = Arrays.copyOf(tab, j + 1);
    return B;
}
like image 988
David Lund Avatar asked Dec 25 '22 17:12

David Lund


2 Answers

This works because the code under

if (tab[i] == tab[j])

iterates through the sorted array, skipping duplicated elements and instead copying each unique element forward to the front part of the array, just after the already scanned (and known to be unique elements). It then only keeps that front part of the array.

Stepping through the code:

if (tab.length < 2)
    return tab;

int j = 0;
int i = 1;

Method gets input: [1,1,1,2,2,2,3], is sorted (since the input is already sorted in this example, there is no change), tab is greater than 2, so do not return. j is assigned the value 0 i is assigned the value 1

while (i < tab.length) { ... }

i (which is 1) is less than the tab length (which is 7). While loop is entered:

    if (tab[i] == tab[j]) {
        i++;
    } else {
        j++;
        tab[j] = tab[i];
        i++;
    }       

Iteration 1: tab[i], which is tab[1], which is 1, is compared to tab[j], which is tab[0], which is 1. They are equal, so i is incremented. i is now 2.

Iteration 2: tab[i] (tab[2], or 1) is compared to tab[j] (tab[0] or 1). They are equal, so i is incremented. i is now 3.

Iteration 3: tab[i] (tab[3], or 2) is compared to tab[j] (tab[0] or 1). They are not equal. j is incremented, and is now 1. tab[j] (tab[1]) is assigned the value of tab[i] (tab[3]). tab is now [1,2,1,2,2,2,3]. i is incremented, and is now 4.

Iteration 4: tab[i] (tab[4], or 2) is compared to tab[j] (tab[1] or 2). They are equal, so i is incremented. i is now 5.

Iteration 5: tab[i] (tab[5], or 2) is compared to tab[j] (tab[1] or 2). They are equal, so i is incremented. i is now 6.

Iteration 6: tab[i] (tab[6], or 3) is compared to tab[j] (tab[1] or 2). They are not equal. j is incremented, and is now 2. tab[j] (tab[2]) is assigned the value of tab[i] (tab[6]). tab is now [1,2,3,2,2,2,3]. i is incremented, and is now 7.

I is now no longer less than the length of tab, we exit the while loop.

int[] B = Arrays.copyOf(tab, j + 1);
return B;

B is created by copying tab up to length j + 1, or 3, starting from the first element. B is now [1,2,3].

Method returns [1,2,3], as expected.

like image 83
Brandon McKenzie Avatar answered Jan 14 '23 02:01

Brandon McKenzie


Dig this simulation:

Start
[1, 1, 1, 2, 2, 2, 3]
 j  i
Duplicate found (tab[i] == tab[j]), move i over 1 (i++)
[1, 1, 1, 2, 2, 2, 3]
 j     i
Duplicate found, move i over 1
[1, 1, 1, 2, 2, 2, 3]
 j        i
Non-duplicate (else); adding 1 to j (j++), 
    copying element i to el j (tab[j] = tab[i]), 
    adding 1 to i (i++)
[1, 2, 1, 2, 2, 2, 3]
    j        i
Duplicate found, move i over 1
[1, 2, 1, 2, 2, 2, 3]
    j           i
Duplicate found, move i over 1
[1, 2, 1, 2, 2, 2, 3]
    j              i
Non-duplicate; adding 1 to j, copying i to j, adding 1 to i
[1, 2, 3, 2, 2, 2, 3]
       j              i
i == tab.length, so stop
Copy first 3 elements of array (up to j) to result 
    (int[] B = Arrays.copyOf(tab, j + 1)) and return it
like image 38
Reinstate Monica -- notmaynard Avatar answered Jan 14 '23 02:01

Reinstate Monica -- notmaynard