Skip to main content
Marcos_Suárez
Known Participant
November 19, 2010
Answered

JS: How to remove duplicate items from an Array?

  • November 19, 2010
  • 2 replies
  • 23526 views

JS: How to remove duplicate items from an Array?

I guess there will be a simple way ...

This topic has been closed for replies.
Correct answer tomaxxi

Hey!

Maybe something like this:

Array.prototype.unique = function (){
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++){
        for(var x = 0, y = r.length; x < y; x++){
            if(r==this) continue o;}
        r[r.length] = this;}
    return r;
}

Usage:

var myArray = ["a","b","c","c","a","d","b","b"];
alert(myArray.unique());

Hope that helps.

--

tomaxxi

http://indisnip.wordpress.com/

2 replies

Harbs.
Legend
November 19, 2010

I use Peter Kahrel's arrayCompress function for string arrays...

function arrayCompress(array){
    var str = array.sort().join('\r')+'\r';
    str = str.replace(/([^\r]+\r)(\1)+/g,'$1');

    str = str.replace(/\r$/,'');
    return str.split('\r');

}

Harbs

Marcos_Suárez
Known Participant
November 19, 2010

OK...

Very usefull funtions...!!!

THANKS...!!!

Marc Autret
Legend
November 20, 2010

@Marijan

Your implementation is OK but it has a quadratic time complexity -- O(n²) -- because of the loop within the loop.

On large arrays (+1000 items), the "Hash Sieving” method is faster -- O(2n). Here is a functional implementation used for string arrays:

function unique(/*str[]*/ arr)
{
     var o={},
          r=[],
          n = arr.length,
          i;

     for( i=0 ; i<n ; ++i )
          o[arr] = null;

     for( i in o )
          r.push(i);

     return r;
}

// TEST:
alert( unique(["a","b","c","c","a","d","b","b"]) );
// => a, b, c, d

@Harbs

The arrayCompress function is very original, but I am skeptical about its performance on large arrays.

Also, there is two important issues to mention:

1) the original array is not preserved, because array.sort() reorders its own elements -- we can fix this using array.concat().sort().

2) the function does not work if any of the supplied strings already contains "\r".

@+

Marc

tomaxxi
tomaxxiCorrect answer
Inspiring
November 19, 2010

Hey!

Maybe something like this:

Array.prototype.unique = function (){
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++){
        for(var x = 0, y = r.length; x < y; x++){
            if(r==this) continue o;}
        r[r.length] = this;}
    return r;
}

Usage:

var myArray = ["a","b","c","c","a","d","b","b"];
alert(myArray.unique());

Hope that helps.

--

tomaxxi

http://indisnip.wordpress.com/