Kaloyan Stamatov
Kaloyan Stamatov

Reputation: 4024

Can someone explain me how this function for removing duplicate values in array work?

I have been looking for a javascript function that remove duplicate values in array. I found this function:

function removeDuplicateElement(arrayName)
{
    var newArray=new Array();
    label:for(var i=0; i<arrayName.length;i++ )
    {   
        for(var j=0; j<newArray.length;j++ )
        {
            if(newArray[j]==arrayName[i]) 
            continue label;
        }
        newArray[newArray.length] = arrayName[i];
    }
    return newArray;
}

which is what i need. Can someone explain me how this function actually work and for what that 'label:' is? I cant understand the logic of this code and if someone can give me an explanation it will be great. 10x

Upvotes: 2

Views: 557

Answers (4)

Chris Moschini
Chris Moschini

Reputation: 37967

FYI Sizzle's method is faster:

function dedupe(array) {
    array.sort();
    for(var i = 1; i < array.length; i++) {
        if (array[i-1] == array[i])
            array.splice(i--, 1);
    }

    return array;
}

Although I was actually tweaking this to see if I could speed it up any on jsperf.com - this is actually faster than either:

function dedupeManyDupes(array) {
    array.sort();
    var l = array.length;
    for(var i = 1; i < l; i++) {
        if (array[i-1] == array[i]) {
            var dupe = array[i];
            var mark = i;
            for(i++; i < l && array[i] == dupe; i++)
            {}
            var snip = i - mark;
            array.splice(mark, snip);
            i -= snip;
            l -= snip;
        }
    }
    return array;
}

http://jsperf.com/js-dedupe-small
http://jsperf.com/js-dedupe-large
http://jsperf.com/js-dedupe-many-dupes

http://jsfiddle.net/b9chris/fLBTv/

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270727

In JavaScript, you can specify a label: as a location to jump to in a continue or break statement. So when the continue is reached, the iteration returns to the outer for loop instead of the inner loop where it resides (which would be the default behavior of continue)

Basically, this function works by making a new array newArray (not modifying the old one) and looping over every element in the original array. It adds the element from the original array to the newArray if not already found. It determines if it already is present in newArray by looping over that it for each iteration of the old array's loop and looking for the matching value arrayName[i]

function removeDuplicateElement(arrayName)
{
    // Declares a new array to hold the deduped values
    var newArray=new Array();
    // Loops over the original array
    // label: here defines a point for the continue statement to target
    label:for(var i=0; i<arrayName.length;i++ )
    {   
        // Loops over the new array to see if the current value from the old array
        // already exists here
        for(var j=0; j<newArray.length;j++ )
        {
            // The new array already has the current loop val from the old array
            if(newArray[j]==arrayName[i]) 
            // So it returns to the outer loop taking no further action 
            // This advances the outer loop to its next iteration
            continue label;
        }
        // Otherwise, the current value is added to the new array
        newArray[newArray.length] = arrayName[i];
    }
    // The new deduped array is returned from the function
    return newArray;
}

For more information on the function of the continue in this context, have a look at the MDN documentation.

Upvotes: 3

Šime Vidas
Šime Vidas

Reputation: 186013

You can simplify that a bit with ES5 methods:

function dumpDupes ( arr ) {
    var arr2 = [];
    arr.forEach(function ( item ) {
        if ( arr2.indexOf( item ) === -1 ) arr2.push( item );
    });
    return arr2;
}

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182827

The function loops through all the elements in the existing array and adds them to the new array if they're not already in it. The label allows the inner loop to use the continue statement to continue with the next iteration of the outer loop rather than the next iteration of the inner loop. A regular continue would just repeat the inner loop, which is what would happen anyway.

Upvotes: 1

Related Questions