How does this code remove duplicates from a sorted array?

If I pass in a number, like an array 2, 3, 3, 3, 1. It removes duplicates from 3, but why? and the result is 123 (which should be related to the sorting method).

Sortering.sorterHeltallstabell(tab)sorts only my code, and the rest removes duplicates. It is sorted before removing duplicates.

Why does this code remove duplicate arrays when passing it to a 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;
}
+4
source share
4 answers

Run 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
+3
source

This works because the code under

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

, ( ). .

:

if (tab.length < 2)
    return tab;

int j = 0;
int i = 1;

: [1,1,1,2,2,2,3], ( , ), 2, . j 0 i 1

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

i ( 1) ( 7). :

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

1: tab [i], [1], 1, tab [j], [0], 1. , . 2.

2: [i] ( [2] 1) [j] ( [0] 1). , . 3.

3: [i] ( [3] 2) [j] ( [0] 1). . j 1. tab [j] ( [1]) tab [i] ( [3]). [1,2,1,2,2,2,3]. 4.

4: [i] ( [4] 2) [j] ( [1] 2). , . 5.

5: [i] ( [5] 2) tab [j] ( [1] 2). , . 6.

6: [i] ( [6] 3) [j] ( [1] 2). . j , 2. tab [j] ( [2]) tab [i] ( [6]). [1,2,3,2,2,2,3]. , 7.

, while.

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

B j + 1 3, . B [1,2,3].

[1,2,3], .

+3

,

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

, .

+2

.

  • . ( ) . Java ( , ), . , (, #), .

  • , . , , ( , , , ).

Strongly commented explanation

public static int[] utenDubletter(int[] tab){
    //sort the array
    Sortering.sorterHeltallstabell(tab);

    //There must be at least 2 elements for any duplicate to exist
    if (tab.length < 2)
        return tab;

    int j = 0; //index of the largest element in the new array found so far
    int i = 1; //index of the current index of the array being checked

    while (i < tab.length) {
        //if it a duplicate 
        if (tab[i] == tab[j]) {
            i++;    //just skip this element and check the next one
        } else {
            j++;    //since this number does not exist in the new array make space for it 
            tab[j] = tab[i];    //record this new element
                                //we have checked this element (with i) before this 
                                //so we don't need to keep it around any longer
            i++;    //move onto the next element 
        }           
    }              
    int[] B = Arrays.copyOf(tab, j + 1);    //copy only the elements that we actually 
                                            //manually overwrote. Since arrays are
                                            //0-indexed, add one to the final index (j)
                                            //for the number of elements in our new array.
    return B;
}
+2
source

All Articles