Deleting both values from array if duplicate - JavaScript/jQuery

JavaScript performance comparison

Test case created by Murtnowski

Info

Algorithms to remove all entries from an array which contain duplicates.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var myArr = [1, 1, 2, 5, 5, 7, 8, 9, 9];
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Richard JP Le Guen
function removeDuplicates(arr) {
    var counts = arr.reduce(function(counts, item) {
        counts[item] = (counts[item]||0)+1;
        return counts;
    }, {});
    return Object.keys(counts).reduce(function(arr, item) {
        if(counts[item] === 1) {
            arr.push(item);
        }
        return arr;
    }, []);
}

removeDuplicates(myArr);
pending…
Murtnowski
var map = new Object();

for(var i = 0; i < myArr.length; i++)
{
    if(map[myArr[i]] === undefined)
    {
        map[myArr[i]] = 1;
    }
    else
    {
        map[myArr[i]]++;
    }
}

var result = new Array();

for(var i = 0; i < myArr.length; i++)
{  
    if(map[myArr[i]] > 1)
    {
        //do nothing
    }
    else
    {
        result.push(myArr[i]);
    }

}
pending…
Richard JP Le Guen 2
function removeDuplicates(arr) {
    var counts = arr.reduce(function(counts, item) {
        counts[item] = (counts[item]||0)+1;
        return counts;
    }, {});
    var arr = [];
    for(item in counts) {
        if(counts[item] === 1) {
            arr.push(item);
        }
    }
    return arr;
}

removeDuplicates(myArr);
pending…
Adil
for (i = 0; i < myArr.length - 1; i++) {
    debugger
    found = false;
    for (j = i + 1; j < myArr.length; j++)
    if (myArr[i] == myArr[j]) {
        myArr.splice(j, 1)
        found = true;
    }

    if (found) myArr.splice(i, 1);
}
pending…
meouw
function removeIfduplicate( arr ) {
    var discarded = [];
    var good      = [];
    var test;
    while( test = arr.pop() ) {
        if( arr.indexOf( test ) > -1 ) {
            discarded.push( test );
            continue;
        } else if( discarded.indexOf( test ) == -1 ) {
            good.push( test );
        }
    }
    return good.reverse();
}

x = removeIfduplicate( myArr );
pending…
theJoelFisch
function removeDuplicates(arr) {
    var i, tmp;
    for(i=0; i<arr.length; i++) {
        tmp = arr.lastIndexOf(arr[i]);
        if(tmp === i) {
            //Only one of this number
        } else {
            //More than one
            arr.splice(tmp, 1);
            arr.splice(i, 1);
        }
    }
}

removeDuplicates( myArr );
pending…
BumbleB2na
var newArr = myArr;

for(var h = 0; h < myArr.length; h++) {
    var curItem = myArr[h];
    var foundCount = 0;
    // search array for item
    for(var i = 0; i < myArr.length; i++) {
        if (myArr[i] == myArr[h])
            foundCount++;
    }
    if(foundCount > 1) {
        // remove repeated item from new array
        for(var j = 0; j < newArr.length; j++) {
            if(newArr[j] == curItem) {                
                newArr.splice(j, 1);
                j = j - 1;
            }
        }            
    }
}
pending…
Richard Neil Ilagan
clean_array = $.map(myArr.sort(), function (v,i) {
        myArr[i] === myArr[i+1] && (myArr[i] = myArr[i+1] = null);
        return myArr[i];
    });
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment