Remove duplicate values from JS array [duplicate] Remove duplicate values from JS array [duplicate] arrays arrays

Remove duplicate values from JS array [duplicate]


TL;DR

Using the Set constructor and the spread syntax:

uniq = [...new Set(array)];

"Smart" but naïve way

uniqueArray = a.filter(function(item, pos) {    return a.indexOf(item) == pos;})

Basically, we iterate over the array and, for each element, check if the first position of this element in the array is equal to the current position. Obviously, these two positions are different for duplicate elements.

Using the 3rd ("this array") parameter of the filter callback we can avoid a closure of the array variable:

uniqueArray = a.filter(function(item, pos, self) {    return self.indexOf(item) == pos;})

Although concise, this algorithm is not particularly efficient for large arrays (quadratic time).

Hashtables to the rescue

function uniq(a) {    var seen = {};    return a.filter(function(item) {        return seen.hasOwnProperty(item) ? false : (seen[item] = true);    });}

This is how it's usually done. The idea is to place each element in a hashtable and then check for its presence instantly. This gives us linear time, but has at least two drawbacks:

  • since hash keys can only be strings or symbols in JavaScript, this code doesn't distinguish numbers and "numeric strings". That is, uniq([1,"1"]) will return just [1]
  • for the same reason, all objects will be considered equal: uniq([{foo:1},{foo:2}]) will return just [{foo:1}].

That said, if your arrays contain only primitives and you don't care about types (e.g. it's always numbers), this solution is optimal.

The best from two worlds

A universal solution combines both approaches: it uses hash lookups for primitives and linear search for objects.

function uniq(a) {    var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];    return a.filter(function(item) {        var type = typeof item;        if(type in prims)            return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);        else            return objs.indexOf(item) >= 0 ? false : objs.push(item);    });}

sort | uniq

Another option is to sort the array first, and then remove each element equal to the preceding one:

function uniq(a) {    return a.sort().filter(function(item, pos, ary) {        return !pos || item != ary[pos - 1];    });}

Again, this doesn't work with objects (because all objects are equal for sort). Additionally, we silently change the original array as a side effect - not good! However, if your input is already sorted, this is the way to go (just remove sort from the above).

Unique by...

Sometimes it's desired to uniquify a list based on some criteria other than just equality, for example, to filter out objects that are different, but share some property. This can be done elegantly by passing a callback. This "key" callback is applied to each element, and elements with equal "keys" are removed. Since key is expected to return a primitive, hash table will work fine here:

function uniqBy(a, key) {    var seen = {};    return a.filter(function(item) {        var k = key(item);        return seen.hasOwnProperty(k) ? false : (seen[k] = true);    })}

A particularly useful key() is JSON.stringify which will remove objects that are physically different, but "look" the same:

a = [[1,2,3], [4,5,6], [1,2,3]]b = uniqBy(a, JSON.stringify)console.log(b) // [[1,2,3], [4,5,6]]

If the key is not primitive, you have to resort to the linear search:

function uniqBy(a, key) {    var index = [];    return a.filter(function (item) {        var k = key(item);        return index.indexOf(k) >= 0 ? false : index.push(k);    });}

In ES6 you can use a Set:

function uniqBy(a, key) {    let seen = new Set();    return a.filter(item => {        let k = key(item);        return seen.has(k) ? false : seen.add(k);    });}

or a Map:

function uniqBy(a, key) {    return [        ...new Map(            a.map(x => [key(x), x])        ).values()    ]}

which both also work with non-primitive keys.

First or last?

When removing objects by a key, you might to want to keep the first of "equal" objects or the last one.

Use the Set variant above to keep the first, and the Map to keep the last:

function uniqByKeepFirst(a, key) {    let seen = new Set();    return a.filter(item => {        let k = key(item);        return seen.has(k) ? false : seen.add(k);    });}function uniqByKeepLast(a, key) {    return [        ...new Map(            a.map(x => [key(x), x])        ).values()    ]}//data = [    {a:1, u:1},    {a:2, u:2},    {a:3, u:3},    {a:4, u:1},    {a:5, u:2},    {a:6, u:3},];console.log(uniqByKeepFirst(data, it => it.u))console.log(uniqByKeepLast(data, it => it.u))