7

I have an array of numbers, like: [1, 4, 7, 1, 2, 1, 3, 1, 4].

I would like to remove duplicate elements and sort the result, i.e. the required result is:
[1, 2, 3, 4, 7].

Is there any built in Javascript/jQuery functions to do this, or I must write my own ?

vsync
  • 118,978
  • 58
  • 307
  • 400
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

4 Answers4

8

No, there's nothing built-in. Also, you need to be aware that the default sort is lexical, so [9, 1, 10].sort() will return [1, 10, 9].

The following will sort and remove duplicates from an array of numbers in place:

function sortAndRemoveDuplicates(arr) {
    arr.sort( function(a, b) { return a - b; } );
    var copy = arr.slice(0);
    arr.length = 0;

    for (var i = 0, len = copy.length; i < len; ++i) {
        if (i == 0 || copy[i] != copy[i - 1]) {
            arr.push(copy[i]);
        }
    }
    return arr;
}

var arr = [1, 4, 7, 1, 2, 1, 3, 10, 1, 4, 10];
sortAndRemoveDuplicates(arr);
console.log(arr); // [1, 2, 3, 4, 7, 10]
Tim Down
  • 318,141
  • 75
  • 454
  • 536
4

The Underscore library is great for all those kinds of tricks; I love it and wouldn't be able to live without it!!

Once you declare it, you call its functions by using the underscore, like this:

_.uniq([1, 4, 7, 1, 2, 1, 3, 1, 4]);
=> [1, 4, 7, 2, 3]

If you want it sorted:

_.uniq([1, 4, 7, 1, 2, 1, 3, 1, 4]).sort();
=> [1, 2, 3, 4, 7]

From the page linked above:

"Underscore provides 60-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on."

Bambax
  • 2,920
  • 6
  • 34
  • 43
  • +1 for Underscore -- but if the OP is asking for a javascript function that does what `uniq` does, why not pull it out to solve his problem, and then point out that all this and more can be found in the library? :-) – Sean Vieira Sep 10 '10 at 15:27
  • 2
    Well... _.uniq uses _.reduce which uses _.each... That would not be very explicit... – Bambax Sep 10 '10 at 15:43
1

As of JavaScript 1.6, array.filter() can be used to remove duplicate values:

[1, 4, 7, 1, 2, 1, 3, 1, 4]
    .filter(function (value, index, self) {
        return self.indexOf(value) === index;
});

returns

[1, 4, 7, 2, 3]

What's nice about filter is it works on all value types in the array:

[1, "d", 4, "e", 7, "e", 1, "a", 2, "d", 1, "b", 3, "c", 1, "e", 4]
    .filter(function (value, index, self) {
        return self.indexOf(value) === index;
    });

returns

[1, 2, 3, 4, 7, "a", "b", "c", "d", "e"]
Erik Anderson
  • 4,915
  • 3
  • 31
  • 30
1
Array.prototype.unique = function(){
    for(var i = 0; i < this.length; i++){
        if( this.indexOf(this[i], i+1) != -1 ){
            this.splice(i,1);
            i--;
        }
    }
    return this.sort();
}

var x = [1,'x', 4, 7, 1, 2, 1,'x', 3, 1, 4];
x.unique() // [1, 2, 3, 4, 7, "x"]
vsync
  • 118,978
  • 58
  • 307
  • 400