Get all unique values in a JavaScript array (remove duplicates)

ID : 390

viewed : 214

Tags : javascriptuniquearraysjavascript

Top 5 Answer for Get all unique values in a JavaScript array (remove duplicates)

vote vote


With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

function onlyUnique(value, index, self) {   return self.indexOf(value) === index; }  // usage example: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter(onlyUnique);  console.log(unique); // ['a', 1, 2, '1']

The native method filter will loop through the array and leave only those entries that pass the given callback function onlyUnique.

onlyUnique checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.

This solution works without any extra library like jQuery or prototype.js.

It works for arrays with mixed value types too.

For old Browsers (<ie9), that do not support the native methods filter and indexOf you can find work arounds in the MDN documentation for filter and indexOf.

If you want to keep the last occurrence of a value, simply replace indexOf with lastIndexOf.

With ES6 this can be shorten to:

// usage example: var myArray = ['a', 1, 'a', 2, '1']; var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);  console.log(unique); // unique is ['a', 1, 2, '1']

Thanks to Camilo Martin for hint in comment.

ES6 has a native object Set to store unique values. To get an array with unique values you could now do this:

var myArray = ['a', 1, 'a', 2, '1'];  let unique = [ Set(myArray)];  console.log(unique); // unique is ['a', 1, 2, '1']

The constructor of Set takes an iterable object, like an Array, and the spread operator ... transform the set back into an Array. Thanks to Lukas Liese for hint in comment.

vote vote


Updated answer for ES6/ES2015: Using the Set and the spread operator (thanks le-m), the single line solution is:

let uniqueItems = [ Set(items)] 

Which returns

[4, 5, 6, 3, 2, 23, 1] 
vote vote


I split all answers to 4 possible solutions:

  1. Use object { } to prevent duplicates
  2. Use helper array [ ]
  3. Use filter + indexOf
  4. Bonus! ES6 Sets method.

Here's sample codes found in answers:

Use object { } to prevent duplicates

function uniqueArray1( ar ) {   var j = {};    ar.forEach( function(v) {     j[v+ '::' + typeof v] = v;   });    return Object.keys(j).map(function(v){     return j[v];   }); }  

Use helper array [ ]

function uniqueArray2(arr) {     var a = [];     for (var i=0, l=arr.length; i<l; i++)         if (a.indexOf(arr[i]) === -1 && arr[i] !== '')             a.push(arr[i]);     return a; } 

Use filter + indexOf

function uniqueArray3(a) {   function onlyUnique(value, index, self) {        return self.indexOf(value) === index;   }    // usage   var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']    return unique; } 

Use ES6 [ Set(a)]

function uniqueArray4(a) {   return [ Set(a)]; } 

And I wondered which one is faster. I've made sample Google Sheet to test functions. Note: ECMA 6 is not avaliable in Google Sheets, so I can't test it.

Here's the result of tests: enter image description here

I expected to see that code using object { } will win because it uses hash. So I'm glad that tests showed the best results for this algorithm in Chrome and IE. Thanks to @rab for the code.

Update 2020

Google Script enabled ES6 Engine. Now I tested the last code with Sets and it appeared faster than the object method.

vote vote


You can also use underscore.js.

console.log(_.uniq([1, 2, 1, 3, 1, 4]));
<script src=""></script>

which will return:

[1, 2, 3, 4] 
vote vote


One Liner, Pure JavaScript

With ES6 syntax

list = list.filter((x, i, a) => a.indexOf(x) == i)

x --> item in array i --> index of item a --> array reference, (in this case "list") 

enter image description here

With ES5 syntax

list = list.filter(function (x, i, a) {      return a.indexOf(x) == i;  }); 

Browser Compatibility: IE9+

Top 3 video Explaining Get all unique values in a JavaScript array (remove duplicates)