Get all unique values in a JavaScript array (remove duplicates)
JavascriptUniqueArraysJavascript Problem Overview
I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it, but it doesn't fail.
So for the sake of helping me learn, can someone help me determine where the prototype script is going wrong?
Array.prototype.getUnique = function() {
var o = {}, a = [], i, e;
for (i = 0; e = this[i]; i++) {o[e] = 1};
for (e in o) {a.push (e)};
return a;
}
More answers from duplicate question:
Similar question:
Javascript Solutions
Solution 1 - Javascript
With JavaScript 1.6 / ECMAScript 5 you can use the native [filter
][1] 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][1] and [indexOf][2].
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][3] for hint in comment.
ES6 has a native object [Set
][5] 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 = [...new 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][4] for hint in comment.
[1]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter "filter" [2]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf [3]: https://stackoverflow.com/users/124119/camilo-martin [4]: https://stackoverflow.com/users/1737158/lukas-liesis [5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Solution 2 - Javascript
Updated answer for ES6/ES2015: Using the Set and the spread operator (thanks le-m), the single line solution is:
let uniqueItems = [...new Set(items)]
Which returns
[4, 5, 6, 3, 2, 23, 1]
Solution 3 - Javascript
I split all answers to 4 possible solutions:
- Use object
{ }
to prevent duplicates - Use helper array
[ ]
- Use
filter + indexOf
- 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 [...new Set(a)]
function uniqueArray4(a) {
return [...new 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.
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.
Solution 4 - Javascript
You can also use underscore.js.
console.log(_.uniq([1, 2, 1, 3, 1, 4]));
<script src="http://underscorejs.org/underscore-min.js"></script>
which will return:
[1, 2, 3, 4]
Solution 5 - Javascript
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")
With ES5 syntax
list = list.filter(function (x, i, a) {
return a.indexOf(x) == i;
});
Browser Compatibility: IE9+
Solution 6 - Javascript
Many of the answers here may not be useful to beginners. If de-duping an array is difficult, will they really know about the prototype chain, or even jQuery?
In modern browsers, a clean and simple solution is to store data in a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set "Sets in JavaScript"), which is designed to be a list of unique values.
const cars = ['Volvo', 'Jeep', 'Volvo', 'Lincoln', 'Lincoln', 'Ford'];
const uniqueCars = Array.from(new Set(cars));
console.log(uniqueCars);
The Array.from
is useful to convert the Set back to an Array so that you have easy access to all of the awesome methods (features) that arrays have. There are also other ways of doing the same thing. But you may not need Array.from
at all, as Sets have plenty of useful features like [forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/forEach "Looping with Sets").
If you need to support old Internet Explorer, and thus cannot use Set, then a simple technique is to copy items over to a new array while checking beforehand if they are already in the new array.
// Create a list of cars, with duplicates.
var cars = ['Volvo', 'Jeep', 'Volvo', 'Lincoln', 'Lincoln', 'Ford'];
// Create a list of unique cars, to put a car in if we haven't already.
var uniqueCars = [];
// Go through each car, one at a time.
cars.forEach(function (car) {
// The code within the following block runs only if the
// current car does NOT exist in the uniqueCars list
// - a.k.a. prevent duplicates
if (uniqueCars.indexOf(car) === -1) {
// Since we now know we haven't seen this car before,
// copy it to the end of the uniqueCars list.
uniqueCars.push(car);
}
});
To make this instantly reusable, let's put it in a function.
function deduplicate(data) {
if (data.length > 0) {
var result = [];
data.forEach(function (elem) {
if (result.indexOf(elem) === -1) {
result.push(elem);
}
});
return result;
}
}
So to get rid of the duplicates, we would now do this.
var uniqueCars = deduplicate(cars);
The deduplicate(cars)
part becomes the thing we named result when the function completes.
Just pass it the name of any array you like.
Solution 7 - Javascript
I have since found a nice method that uses jQuery
arr = $.grep(arr, function(v, k){
return $.inArray(v ,arr) === k;
});
Note: This code was pulled from Paul Irish's duck punching post - I forgot to give credit :P
Solution 8 - Javascript
Using ES6 new Set
var array = [3,7,5,3,2,5,2,7];
var unique_array = [...new Set(array)];
console.log(unique_array); // output = [3,7,5,2]
Using For Loop
var array = [3,7,5,3,2,5,2,7];
for(var i=0;i<array.length;i++) {
for(var j=i+1;j<array.length;j++) {
if(array[i]===array[j]) {
array.splice(j,1);
}
}
}
console.log(array); // output = [3,7,5,2]
Solution 9 - Javascript
The simplest, and fastest (in Chrome) way of doing this:
Array.prototype.unique = function() {
var a = [];
for (var i=0, l=this.length; i<l; i++)
if (a.indexOf(this[i]) === -1)
a.push(this[i]);
return a;
}
Simply goes through every item in the array, tests if that item is already in the list, and if it's not, pushes to the array that gets returned.
According to JSBench, this function is the fastest of the ones I could find anywhere - feel free to add your own though.
The non-prototype version:
function uniques(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;
}
Sorting
When also needing to sort the array, the following is the fastest:
Array.prototype.sortUnique = function() {
this.sort();
var last_i;
for (var i=0;i<this.length;i++)
if ((last_i = this.lastIndexOf(this[i])) !== i)
this.splice(i+1, last_i-i);
return this;
}
or non-prototype:
function sortUnique(arr) {
arr.sort();
var last_i;
for (var i=0;i<arr.length;i++)
if ((last_i = arr.lastIndexOf(arr[i])) !== i)
arr.splice(i+1, last_i-i);
return arr;
}
This is also faster than the above method in most non-Chrome browsers.
Solution 10 - Javascript
Remove duplicates using Set
.
// Array with duplicates⤵️
const withDuplicates = [2, 2, 5, 5, 1, 1, 2, 2, 3, 3];
// Get new array without duplicates by using Set
// [2, 5, 1, 3]
const withoutDuplicates = Array.from(new Set(arrayWithDuplicates));
Solution 11 - Javascript
["Defects", "Total", "Days", "City", "Defects"].reduce(function(prev, cur) {
return (prev.indexOf(cur) < 0) ? prev.concat([cur]) : prev;
}, []);
[0,1,2,0,3,2,1,5].reduce(function(prev, cur) {
return (prev.indexOf(cur) < 0) ? prev.concat([cur]) : prev;
}, []);
Solution 12 - Javascript
Magic
a.filter(e=>!(t[e]=e in t))
O(n) performance (is faster than new Set
); we assume your array is in a
and t={}
. Explanation here (+Jeppe impr.)
let t, unique= a=> ( t={}, a.filter(e=>!(t[e]=e in t)) );
// "stand-alone" version working with global t:
// a1.filter((t={},e=>!(t[e]=e in t)));
// Test data
let a1 = [5,6,0,4,9,2,3,5,0,3,4,1,5,4,9];
let a2 = [[2, 17], [2, 17], [2, 17], [1, 12], [5, 9], [1, 12], [6, 2], [1, 12]];
let a3 = ['Mike', 'Adam','Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'];
// Results
console.log(JSON.stringify( unique(a1) ))
console.log(JSON.stringify( unique(a2) ))
console.log(JSON.stringify( unique(a3) ))
Solution 13 - Javascript
We can do this using ES6 sets:
var duplicatedArray = [1, 2, 3, 4, 5, 1, 1, 1, 2, 3, 4];
var uniqueArray = Array.from(new Set(duplicatedArray));
console.log(uniqueArray);
//The output will be
uniqueArray = [1,2,3,4,5];
Solution 14 - Javascript
This prototype getUnique
is not totally correct, because if i have a Array like: ["1",1,2,3,4,1,"foo"]
it will return ["1","2","3","4"]
and "1"
is string and 1
is a integer; they are different.
Here is a correct solution:
Array.prototype.unique = function(a){
return function(){ return this.filter(a) }
}(function(a,b,c){ return c.indexOf(a,b+1) < 0 });
using:
var foo;
foo = ["1",1,2,3,4,1,"foo"];
foo.unique();
The above will produce ["1",2,3,4,1,"foo"]
.
Solution 15 - Javascript
You can simlply use the built-in functions Array.prototype.filter()
and Array.prototype.indexOf()
array.filter((x, y) => array.indexOf(x) == y)
var arr = [1, 2, 3, 3, 4, 5, 5, 5, 6, 7, 8, 9, 6, 9];
var newarr = arr.filter((x, y) => arr.indexOf(x) == y);
console.log(newarr);
Solution 16 - Javascript
After looking into all the 90+ answers here, I saw there is room for one more:
Array.includes has a very handy second-parameter: "fromIndex", so by using it, every iteration of the filter
callback method will search the array, starting from [current index] + 1
which guarantees not to include currently filtered item in the lookup and also saves time.
// 🚩 🚩 🚩
var list = [0,1,2,2,3,'a','b',4,5,2,'a']
console.log(
list.filter((v,i) => !list.includes(v,i+1))
)
// [0,1,3,"b",4,5,2,"a"]
Explanation:
For example, lets assume the filter
function is currently iterating at index 2
) and the value at that index happens to be 2
. The section of the array that is then scanned for duplicates (includes
method) is everything after index 2 (i+1
):
👇 👇
[0, 1, 2, 2 ,3 ,'a', 'b', 4, 5, 2, 'a']
👆 |---------------------------|
And since the currently filtered item's value 2
is included in the rest of the array, it will be filtered out, because of the leading exclamation mark which negates the filter rule.
Solution 17 - Javascript
[...new Set(duplicates)]
> This is the simplest one and referenced from MDN Web Docs.
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
console.log([...new Set(numbers)]) // [2, 3, 4, 5, 6, 7, 32]
Solution 18 - Javascript
Array.prototype.getUnique = function() {
var o = {}, a = []
for (var i = 0; i < this.length; i++) o[this[i]] = 1
for (var e in o) a.push(e)
return a
}
Solution 19 - Javascript
Without extending Array.prototype (it is said to be a bad practice) or using jquery/underscore, you can simply filter
the array.
By keeping last occurrence:
function arrayLastUnique(array) {
return array.filter(function (a, b, c) {
// keeps last occurrence
return c.indexOf(a, b + 1) < 0;
});
},
or first occurrence:
function arrayFirstUnique(array) {
return array.filter(function (a, b, c) {
// keeps first occurrence
return c.indexOf(a) === b;
});
},
Well, it's only javascript ECMAScript 5+, which means only IE9+, but it's nice for a development in native HTML/JS (Windows Store App, Firefox OS, Sencha, Phonegap, Titanium, ...).
Solution 20 - Javascript
That's because 0
is a falsy value in JavaScript.
this[i]
will be falsy if the value of the array is 0 or any other falsy value.
Solution 21 - Javascript
If you're using Prototype framework there is no need to do 'for' loops, you can use http://prototypejs.org/doc/latest/language/Array/prototype/uniq/ like this:
var a = Array.uniq();
Which will produce a duplicate array with no duplicates. I came across your question searching a method to count distinct array records so after uniq()
I used size()
and there was my simple result.
p.s. Sorry if i mistyped something
edit: if you want to escape undefined records you may want to add compact()
before, like this:
var a = Array.compact().uniq();
Solution 22 - Javascript
This has been answered a lot, but it didn't address my particular need.
Many answers are like this:
a.filter((item, pos, self) => self.indexOf(item) === pos);
But this doesn't work for arrays of complex objects.
Say we have an array like this:
const a = [
{ age: 4, name: 'fluffy' },
{ age: 5, name: 'spot' },
{ age: 2, name: 'fluffy' },
{ age: 3, name: 'toby' },
];
If we want the objects with unique names, we should use array.prototype.findIndex
instead of array.prototype.indexOf
:
a.filter((item, pos, self) => self.findIndex(v => v.name === item.name) === pos);
Solution 23 - Javascript
Now using sets you can remove duplicates and convert them back to the array.
var names = ["Mike","Matt","Nancy", "Matt","Adam","Jenny","Nancy","Carl"];
console.log([...new Set(names)])
Another solution is to use sort & filter
var names = ["Mike","Matt","Nancy", "Matt","Adam","Jenny","Nancy","Carl"];
var namesSorted = names.sort();
const result = namesSorted.filter((e, i) => namesSorted[i] != namesSorted[i+1]);
console.log(result);
Solution 24 - Javascript
I had a slightly different problem where I needed to remove objects with duplicate id properties from an array. this worked.
let objArr = [{
id: '123'
}, {
id: '123'
}, {
id: '456'
}];
objArr = objArr.reduce((acc, cur) => [
...acc.filter((obj) => obj.id !== cur.id), cur
], []);
console.log(objArr);
Solution 25 - Javascript
If you're okay with extra dependencies, or you already have one of the libraries in your codebase, you can remove duplicates from an array in place using LoDash (or Underscore).
Usage
If you don't have it in your codebase already, install it using npm:
npm install lodash
Then use it as follows:
import _ from 'lodash';
let idArray = _.uniq ([
1,
2,
3,
3,
3
]);
console.dir(idArray);
Out:
[ 1, 2, 3 ]
Solution 26 - Javascript
I'm not sure why Gabriel Silveira wrote the function that way but a simpler form that works for me just as well and without the minification is:
Array.prototype.unique = function() {
return this.filter(function(value, index, array) {
return array.indexOf(value, index + 1) < 0;
});
};
or in CoffeeScript:
Array.prototype.unique = ->
this.filter( (value, index, array) ->
array.indexOf(value, index + 1) < 0
)
Solution 27 - Javascript
It appears we have lost [Rafael's answer](https://stackoverflow.com/a/1961068/519360 "(broken link)"), which stood as the accepted answer for a few years. This was (at least in 2017) the best-performing solution if you don't have a mixed-type array:
Array.prototype.getUnique = function(){
var u = {}, a = [];
for (var i = 0, l = this.length; i < l; ++i) {
if (u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
If you do have a mixed-type array, you can serialize the hash key:
Array.prototype.getUnique = function() {
var hash = {}, result = [], key;
for ( var i = 0, l = this.length; i < l; ++i ) {
key = JSON.stringify(this[i]);
if ( !hash.hasOwnProperty(key) ) {
hash[key] = true;
result.push(this[i]);
}
}
return result;
}
Solution 28 - Javascript
Finding unique Array values in simple method
function arrUnique(a){
var t = [];
for(var x = 0; x < a.length; x++){
if(t.indexOf(a[x]) == -1)t.push(a[x]);
}
return t;
}
arrUnique([1,4,2,7,1,5,9,2,4,7,2]) // [1, 4, 2, 7, 5, 9]
Solution 29 - Javascript
strange this hasn't been suggested before.. to remove duplicates by object key (id
below) in an array you can do something like this:
const uniqArray = array.filter((obj, idx, arr) => (
arr.findIndex((o) => o.id === obj.id) === idx
))
Solution 30 - Javascript
For an object-based array with some unique id's, I have a simple solution through which you can sort in linear complexity
function getUniqueArr(arr){
const mapObj = {};
arr.forEach(a => {
mapObj[a.id] = a
})
return Object.values(mapObj);
}
Solution 31 - Javascript
Using object keys to make unique array, I have tried following
function uniqueArray( ar ) {
var j = {};
ar.forEach( function(v) {
j[v+ '::' + typeof v] = v;
});
return Object.keys(j).map(function(v){
return j[v];
});
}
uniqueArray(["1",1,2,3,4,1,"foo", false, false, null,1]);
Which returns ["1", 1, 2, 3, 4, "foo", false, null]
Solution 32 - Javascript
The task is to get a unique array from an array consisted of arbitrary types (primitive and non primitive).
The approach based on using new Set(...)
is not new. Here it is leveraged by JSON.stringify(...)
, JSON.parse(...)
and [].map
method. The advantages are universality (applicability for an array of any types), short ES6 notation and probably performance for this case:
const dedupExample = [
{ a: 1 },
{ a: 1 },
[ 1, 2 ],
[ 1, 2 ],
1,
1,
'1',
'1'
]
const getUniqArrDeep = arr => {
const arrStr = arr.map(item => JSON.stringify(item))
return [...new Set(arrStr)]
.map(item => JSON.parse(item))
}
console.info(getUniqArrDeep(dedupExample))
/* [ {a: 1}, [1, 2], 1, '1' ] */
Solution 33 - Javascript
As explained already, [...new Set(values)]
is the best option, if that's available to you.
Otherwise, here's a one-liner that doesn't iterate the array for every index:
values.sort().filter((val, index, arr) => index === 0 ? true : val !== arr[index - 1]);
That simply compares each value to the one before it. The result will be sorted.
Example: let values = [ 1, 2, 3, 3, 4, 5, 5, 5, 4, 4, 4, 5, 1, 1, 1, 3, 3 ]; let unique = values.sort().filter((val, index, arr) => index === 0 ? true : val !== arr[index - 1]); console.log(unique);
Solution 34 - Javascript
If anyone using knockoutjs
ko.utils.arrayGetDistinctValues()
BTW have look at all ko.utils.array*
utilities.
Solution 35 - Javascript
Building on other answers, here's another variant that takes an optional flag to choose a strategy (keep first occurrence or keep last):
Without extending Array.prototype
function unique(arr, keepLast) {
return arr.filter(function (value, index, array) {
return keepLast ? array.indexOf(value, index + 1) < 0 : array.indexOf(value) === index;
});
};
// Usage
unique(['a', 1, 2, '1', 1, 3, 2, 6]); // -> ['a', 1, 2, '1', 3, 6]
unique(['a', 1, 2, '1', 1, 3, 2, 6], true); // -> ['a', '1', 1, 3, 2, 6]
Extending Array.prototype
Array.prototype.unique = function (keepLast) {
return this.filter(function (value, index, array) {
return keepLast ? array.indexOf(value, index + 1) < 0 : array.indexOf(value) === index;
});
};
// Usage
['a', 1, 2, '1', 1, 3, 2, 6].unique(); // -> ['a', 1, 2, '1', 3, 6]
['a', 1, 2, '1', 1, 3, 2, 6].unique(true); // -> ['a', '1', 1, 3, 2, 6]
Solution 36 - Javascript
Finding unique in Array of objects using One Liner
const uniqueBy = (x,f)=>Object.values(x.reduce((a,b)=>((a[f(b)]=b),a),{}));
// f -> should must return string because it will be use as key
const data = [
{ comment: "abc", forItem: 1, inModule: 1 },
{ comment: "abc", forItem: 1, inModule: 1 },
{ comment: "xyz", forItem: 1, inModule: 2 },
{ comment: "xyz", forItem: 1, inModule: 2 },
];
uniqueBy(data, (x) => x.forItem +'-'+ x.inModule); // find unique by item with module
// output
// [
// { comment: "abc", forItem: 1, inModule: 1 },
// { comment: "xyz", forItem: 1, inModule: 2 },
// ];
// can also use for strings and number or other primitive values
uniqueBy([1, 2, 2, 1], (v) => v); // [1, 2]
uniqueBy(["a", "b", "a"], (v) => v); // ['a', 'b']
uniqueBy(
[
{ id: 1, name: "abc" },
{ id: 2, name: "xyz" },
{ id: 1, name: "abc" },
],
(v) => v.id
);
// output
// [
// { id: 1, name: "abc" },
// { id: 2, name: "xyz" },
// ];
Solution 37 - Javascript
You can also use jQuery
var a = [1,5,1,6,4,5,2,5,4,3,1,2,6,6,3,3,2,4];
// note: jQuery's filter params are opposite of javascript's native implementation :(
var unique = $.makeArray($(a).filter(function(i,itm){
// note: 'index', not 'indexOf'
return i == $(a).index(itm);
}));
// unique: [1, 5, 6, 4, 2, 3]
Originally answered at: https://stackoverflow.com/questions/5381621/jquery-function-to-get-all-unique-elements-from-an-array
Solution 38 - Javascript
Look at this. Jquery provides uniq method: https://api.jquery.com/jQuery.unique/
var ids_array = []
$.each($(my_elements), function(index, el) {
var id = $(this).attr("id")
ids_array.push(id)
});
var clean_ids_array = jQuery.unique(ids_array)
$.each(clean_ids_array, function(index, id) {
elment = $("#" + id) // my uniq element
// TODO WITH MY ELEMENT
});
Solution 39 - Javascript
Do it with lodash and identity lambda function, just define it before use your object
const _ = require('lodash');
...
_.uniqBy([{a:1,b:2},{a:1,b:2},{a:1,b:3}], v=>v.a.toString()+v.b.toString())
_.uniq([1,2,3,3,'a','a','x'])
and will have:
[{a:1,b:2},{a:1,b:3}]
[1,2,3,'a','x']
(this is the simplest way )
Solution 40 - Javascript
Deduplication usually requires an equality operator for the given type. However, using an eq
function stops us from utilizing a Set
to determine duplicates in an efficient manner, because Set
falls back to ===
. As you know for sure, ===
doesn't work for reference types. So we're kind if stuck, right?
The way out is simply using a transformer function that allows us to transform a (reference) type into something we can actually lookup using a Set
. We could use a hash function, for instance, or JSON.stringify
the data structure, if it doesn't contain any functions.
Often we only need to access a property, which we can then compare instead of the Object
's reference.
Here are two combinators that meet these requirements:
const dedupeOn = k => xs => {
const s = new Set();
return xs.filter(o =>
s.has(o[k])
? null
: (s.add(o[k]), o[k]));
};
const dedupeBy = f => xs => {
const s = new Set();
return xs.filter(x => {
const r = f(x);
return s.has(r)
? null
: (s.add(r), x);
});
};
const xs = [{foo: "a"}, {foo: "b"}, {foo: "A"}, {foo: "b"}, {foo: "c"}];
console.log(
dedupeOn("foo") (xs)); // [{foo: "a"}, {foo: "b"}, {foo: "A"}, {foo: "c"}]
console.log(
dedupeBy(o => o.foo.toLowerCase()) (xs)); // [{foo: "a"}, {foo: "b"}, {foo: "c"}]
With these combinators we're extremely flexible in handling all kinds of deduplication issues. It's not the fastes approach, but the most expressive and most generic one.
Solution 41 - Javascript
Here is an almost one-liner that is O(n), keeps the first element, and where you can keep the field you are uniq'ing on separate.
This is a pretty common technique in functional programming - you use reduce
to build up an array that you return. Since we build the array like this, we guarantee that we get a stable ordering, unlike the [...new Set(array)]
approach. We still use a Set
to ensure we don't have duplicates, so our accumulator contains both a Set
and the array we are building.
const removeDuplicates = (arr) =>
arr.reduce(
([set, acc], item) => set.has(item) ? [set, acc] : [set.add(item), (acc.push(item), acc)],
[new Set(), []]
)[1]
The above will work for simple values, but not for objects, similarly to how [...new Set(array)]
breaks down. If the items are objects that contain an id
property, you'd do:
const removeDuplicates = (arr) =>
arr.reduce(
([set, acc], item) => set.has(item.id) ? [set, acc] : [set.add(item.id), (acc.push(item), acc)],
[new Set(), []]
)[1]
Solution 42 - Javascript
For removing the duplicates there could be 2 situations. first, all the data are not objects, secondly all the data are objects.
If all the data are any kind of primitive data type like int, float, string etc then you can follow this one
const uniqueArray = [...new Set(oldArray)]
But suppose your array consist JS objects like bellow
{
id: 1,
name: 'rony',
email: '[email protected]'
}
then to get all the unique objects you can follow this
let uniqueIds = [];
const uniqueUsers = oldArray.filter(item => {
if(uniqueIds.includes(item.id)){
return false;
}else{
uniqueIds.push(item.id);
return true;
}
})
You can also use this method to make any kind of array to make unique. Just keep the tracking key on the uniqueIds
array.
Solution 43 - Javascript
You can also use sugar.js:
[1,2,2,3,1].unique() // => [1,2,3]
[{id:5, name:"Jay"}, {id:6, name:"Jay"}, {id: 5, name:"Jay"}].unique('id')
// => [{id:5, name:"Jay"}, {id:6, name:"Jay"}]
Solution 44 - Javascript
Yet another solution for the pile.
I recently needed to make a sorted list unique and I did it using filter that keeps track of the previous item in an object like this:
uniqueArray = sortedArray.filter(function(e) {
if(e==this.last)
return false;
this.last=e; return true;
},{last:null});
Solution 45 - Javascript
The version that accepts selector, should be pretty fast and concise:
function unique(xs, f) {
var seen = {};
return xs.filter(function(x) {
var fx = (f && f(x)) || x;
return !seen[fx] && (seen[fx] = 1);
});
}
Solution 46 - Javascript
This one is not pure, it will modify the array, but this is the fastest one. If yours is faster, then please write in the comments ;)
http://jsperf.com/unique-array-webdeb
Array.prototype.uniq = function(){
for(var i = 0, l = this.length; i < l; ++i){
var item = this[i];
var duplicateIdx = this.indexOf(item, i + 1);
while(duplicateIdx != -1) {
this.splice(duplicateIdx, 1);
duplicateIdx = this.indexOf(item, duplicateIdx);
l--;
}
}
return this;
}
[
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc",
"",2,4,"A","abc"
].uniq() // ["",2,4,"A","abc"]
Solution 47 - Javascript
var a = [1,4,2,7,1,5,9,2,4,7,2]
var b = {}, c = {};
var len = a.length;
for(var i=0;i<len;i++){
a[i] in c ? delete b[a[i]] : b[a[i]] = true;
c[a[i]] = true;
}
// b contains all unique elements
Solution 48 - Javascript
For a array of strings:
function removeDuplicatesFromArray(arr) {
const unique = {};
arr.forEach((word) => {
unique[word] = 1; // it doesn't really matter what goes here
});
return Object.keys(unique);
}
Solution 49 - Javascript
You can use Ramda.js, a functional javascript library to do this:
var unique = R.uniq([1, 2, 1, 3, 1, 4])
console.log(unique)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Solution 50 - Javascript
A lot of people have already mentioned using...
[...new Set(arr)];
And this is a great solution, but my preference is a solution that works with .filter
. In my opinion filter is a more natural way to get unique values. You're effectively removing duplicates, and removing elements from an array is exactly what filter is meant for. It also lets you chain off of .map
, .reduce
and other .filter
calls. I devised this solution...
const unique = () => {
let cache;
return (elem, index, array) => {
if (!cache) cache = new Set(array);
return cache.delete(elem);
};
};
myArray.filter(unique());
The caveat is that you need a closure, but I think this is a worthy tradeoff. In terms of performance, it is more performant than the other solutions I have seen posted that use .filter
, but worse performing than [...new Set(arr)]
.
See also my github package youneek
Solution 51 - Javascript
If you want to only get the unique elements and remove the elements which repeats even once, you can do this:
let array = [2, 3, 4, 1, 2, 8, 1, 1, 2, 9, 3, 5, 3, 4, 8, 4];
function removeDuplicates(inputArray) {
let output = [];
let countObject = {};
for (value of array) {
countObject[value] = (countObject[value] || 0) + 1;
}
for (key in countObject) {
if (countObject[key] === 1) {
output.push(key);
}
}
return output;
}
console.log(removeDuplicates(array));
Solution 52 - Javascript
You don't need .indexOf() at all; you can do this O(n):
function SelectDistinct(array) {
const seenIt = new Set();
return array.filter(function (val) {
if (seenIt.has(val)) {
return false;
}
seenIt.add(val);
return true;
});
}
var hasDuplicates = [1,2,3,4,5,5,6,7,7];
console.log(SelectDistinct(hasDuplicates)) //[1,2,3,4,5,6,7]
If you don't want to use .filter():
function SelectDistinct(array) {
const seenIt = new Set();
const distinct = [];
for (let i = 0; i < array.length; i++) {
const value = array[i];
if (!seenIt.has(value)) {
seenIt.add(value);
distinct.push(value);
}
}
return distinct;
/* you could also drop the 'distinct' array and return 'Array.from(seenIt)', which converts the set object to an array */
}
Solution 53 - Javascript
in my solution, I sort data before filtering :
const uniqSortedArray = dataArray.sort().filter((v, idx, t) => idx==0 || v != t[idx-1]);
Solution 54 - Javascript
var myArray = ["a",2, "a", 2, "b", "1"];
const uniques = [];
myArray.forEach((t) => !uniques.includes(t) && uniques.push(t));
console.log(uniques);
Solution 55 - Javascript
If you want to remove duplicates, return the whole objects and want to use ES6 Set and Map syntax, and also run only one loop, you can try this, to get unique ids:
const collection = [{id:3, name: "A"}, {id:3, name: "B"}, {id:4, name: "C"}, {id:5, name: "D"}]
function returnUnique(itemsCollection){
const itemsMap = new Map();
itemsCollection.forEach(item => {
if(itemsMap.size === 0){
itemsMap.set(item.id, item)
}else if(!itemsMap.has(item.id)){
itemsMap.set(item.id, item)
}
});
return [...new Set(itemsMap.values())];
}
console.log(returnUnique(collection));
Solution 56 - Javascript
Not really a direct literal answer to the original question, because I preferred to have the duplicate values never in the array in the first place. So here's my UniqueArray:
class UniqueArray extends Array {
constructor(...args) {
super(...new Set(args));
}
push(...args) {
for (const a of args) if (!this.includes(a)) super.push(a);
return this.length;
}
unshift(...args) {
for (const a of args.reverse()) if (!this.includes(a)) super.unshift(a);
return this.length;
}
concat(...args) {
var r = new UniqueArray(...this);
for (const a of args) r.push(...a);
return r;
}
}
> a = new UniqueArray(1,2,3,1,2,4,5,1)
UniqueArray(5) [ 1, 2, 3, 4, 5 ]
> a.push(1,4,6)
6
> a
UniqueArray(6) [ 1, 2, 3, 4, 5, 6 ]
> a.unshift(1)
6
> a
UniqueArray(6) [ 1, 2, 3, 4, 5, 6 ]
> a.unshift(0)
7
> a
UniqueArray(7) [ 0, 1, 2, 3, 4, 5, 6]
> a.concat(2,3,7)
UniqueArray(8) [ 0, 1, 2, 3, 4, 5, 6, 7]
Solution 57 - Javascript
let ar = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 2, 1];
let unique = ar.filter((value, index) => {
return ar.indexOf(value) == index;
});
console.log(unique);
Solution 58 - Javascript
There's already bunch of great answers. Here's my approach.
var removeDuplicates = function(nums) {
let filteredArr = [];
nums.forEach((item) => {
if(!filteredArr.includes(item)) {
filteredArr.push(item);
}
})
return filteredArr;
}
Solution 59 - Javascript
For an array of tuples, I'll throw things into a Map and let it do the work. With this approach, you have to be mindful about the key you want to be using:
const arrayOfArraysWithDuplicates = [
[1, 'AB'],
[2, 'CD'],
[3, 'EF'],
[1, 'AB'],
[2, 'CD'],
[3, 'EF'],
[3, 'GH'],
]
const uniqueByFirstValue = new Map();
const uniqueBySecondValue = new Map();
arrayOfArraysWithDuplicates.forEach((item) => {
uniqueByFirstValue.set(item[0], item[1]);
uniqueBySecondValue.set(item[1], item[0]);
});
let uniqueList = Array.from( uniqueByFirstValue, ( [ value, name ] ) => ( [value, name] ) );
console.log('Unique by first value:');
console.log(uniqueList);
uniqueList = Array.from( uniqueBySecondValue, ( [ value, name ] ) => ( [value, name] ) );
console.log('Unique by second value:');
console.log(uniqueList);
Output:
Unique by first value:
[ [ 1, 'AB' ], [ 2, 'CD' ], [ 3, 'GH' ] ]
Unique by second value:
[ [ 'AB', 1 ], [ 'CD', 2 ], [ 'EF', 3 ], [ 'GH', 3 ] ]
Solution 60 - Javascript
If order is not important then we can make an hash and get the keys to make unique array.
var ar = [1,3,4,5,5,6,5,6,2,1];
var uarEle = {};
links.forEach(function(a){ uarEle[a] = 1; });
var uar = keys(uarEle)
uar will be having the unique array elements.
Solution 61 - Javascript
I looked at Joeytje50's code on jsperf who has compared a number of alternatives. His code had many minor typos, which made a difference in the performance and the correctness.
More importantly, he is testing on a very small array. I made an array with 1000 integers. Each integer was 100 times a random integer between 0 and 1000. This makes for about 1000/e = 368 duplicates on the average. The results are at jsperf.
This is a much more realistic scenario of where efficiency might be needed. These changes make dramatic changes in the claims (specifically the code touted as fastest is nowhere near fast). The obvious winners are where hashing techniques are used, with the best one being
Array.prototype.getUnique3 = function(){
var u = Object.create(null), a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(this[i] in u) continue;
a.push(this[i]);
u[this[i]] = 1;
}
return a.length;
}
Solution 62 - Javascript
I know this has been answered to death already... but...
no one has mentioned the javascript implementation of linq.
Then the .distinct()
method can be used - and it makes the code super easy to read.
var Linq = require('linq-es2015');
var distinctValues = Linq.asEnumerable(testValues)
.Select(x)
.distinct()
.toArray();
var testValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 1];
var distinctValues = Enumerable.asEnumerable(testValues)
.distinct()
.toArray();
console.log(distinctValues);
<script src="https://npmcdn.com/linq-es5/dist/linq.js"></script>
Solution 63 - Javascript
If you have an array of objects, and you want a uniqueBy
function, say, by an id field:
function uniqueBy(field, arr) {
return arr.reduce((acc, curr) => {
const exists = acc.find(v => v[field] === curr[field]);
return exists ? acc : acc.concat(curr);
}, [])
}
Solution 64 - Javascript
Making an array of unique arrays, using field[2] as an Id:
const arr = [ ['497', 'Q0', 'WTX091-B06-138', '0', '1.000000000', 'GROUP001'],
['497', 'Q0', 'WTX091-B09-92', '1', '0.866899288', 'GROUP001'],
['497', 'Q0', 'WTX091-B09-92', '2', '0.846036819', 'GROUP001'],
['497', 'Q0', 'WTX091-B09-57', '3', '0.835025326', 'GROUP001'],
['497', 'Q0', 'WTX091-B43-79', '4', '0.765068215', 'GROUP001'],
['497', 'Q0', 'WTX091-B43-56', '5', '0.764211464', 'GROUP001'],
['497', 'Q0', 'WTX091-B44-448', '6', '0.761701704', 'GROUP001'],
['497', 'Q0', 'WTX091-B44-12', '7', '0.761701704', 'GROUP001'],
['497', 'Q0', 'WTX091-B49-128', '8', '0.747434800', 'GROUP001'],
['497', 'Q0', 'WTX091-B18-17', '9', '0.746724770', 'GROUP001'],
['497', 'Q0', 'WTX091-B19-374', '10', '0.733379549', 'GROUP001'],
['497', 'Q0', 'WTX091-B19-344', '11', '0.731421782', 'GROUP001'],
['497', 'Q0', 'WTX091-B09-92', '12', '0.726450470', 'GROUP001'],
['497', 'Q0', 'WTX091-B19-174', '13', '0.712757036', 'GROUP001']
];
arr.filter((val1, idx1, arr) => !!~val1.indexOf(val1[2]) &&
!(arr.filter((val2, idx2) => !!~val2.indexOf(val1[2]) &&
idx2 < idx1).length));
console.log(arr);
Solution 65 - Javascript
This is an ES6 function which removes duplicates from an array of objects, filtering by the specified object property
function dedupe(arr = [], fnCheck = _ => _) {
const set = new Set();
let len = arr.length;
for (let i = 0; i < len; i++) {
const primitive = fnCheck(arr[i]);
if (set.has(primitive)) {
// duplicate, cut it
arr.splice(i, 1);
i--;
len--;
} else {
// new item, add it
set.add(primitive);
}
}
return arr;
}
const test = [
{video:{slug: "a"}},
{video:{slug: "a"}},
{video:{slug: "b"}},
{video:{slug: "c"}},
{video:{slug: "c"}}
]
console.log(dedupe(test, x => x.video.slug));
// [{video:{slug: "a"}}, {video:{slug: "b"}}, {video:{slug: "c"}}]
Solution 66 - Javascript
I have a solution that uses es6 reduce and find array helper methods to remove duplicates.
let numbers = [2, 2, 3, 3, 5, 6, 6];
const removeDups = array => {
return array.reduce((acc, inc) => {
if (!acc.find(i => i === inc)) {
acc.push(inc);
}
return acc;
}, []);
}
console.log(removeDups(numbers)); /// [2,3,5,6]
Solution 67 - Javascript
The Object answer above does not seem to work for me in my use case with Objects.
I have modified it as follows:
var j = {};
this.forEach( function(v) {
var typ = typeof v;
var v = (typ === 'object') ? JSON.stringify(v) : v;
j[v + '::' + typ] = v;
});
return Object.keys(j).map(function(v){
if ( v.indexOf('::object') > -1 ) {
return JSON.parse(j[v]);
}
return j[v];
});
This seems to now work correctly for objects, arrays, arrays with mixed values, booleans, etc.
Solution 68 - Javascript
var numbers = [1, 1, 2, 3, 4, 4];
function unique(dupArray) {
return dupArray.reduce(function(previous, num) {
if (previous.find(function(item) {
return item == num;
})) {
return previous;
} else {
previous.push(num);
return previous;
}
}, [])
}
var check = unique(numbers);
console.log(check);
Solution 69 - Javascript
To filter-out undefined and null values because most of the time you do not need them.
const uniques = myArray.filter(e => e).filter((e, i, a) => a.indexOf(e) === i);
or
const uniques = [...new Set(myArray.filter(e => e))];
Solution 70 - Javascript
Sometimes I need to get unique occurrences from an array of objects. Lodash seems like a nice helper but I don't think filtering an array justifies adding a dependency to a project.
Let's assume the comparison of two objects poses on comparing a property, an id for example.
const a = [{id: 3}, {id: 4}, {id: 3}, {id: 5}, {id: 5}, {id: 5}];
Since we all love one line snippets, here is how it can be done:
a.reduce((acc, curr) => acc.find(e => e.id === curr.id) ? acc : [...acc, curr], [])
Solution 71 - Javascript
This solution should be very fast, and will work in many cases.
-
Convert the indexed array items to object keys
-
Use Object.keys function
var indexArray = ["hi","welcome","welcome",1,-9]; var keyArray = {}; indexArray.forEach(function(item){ keyArray[item]=null; }); var uniqueArray = Object.keys(keyArray);
Solution 72 - Javascript
I have a simple example where we can remove objects from array having repeated id in objects,
let data = new Array({id: 1},{id: 2},{id: 3},{id: 1},{id: 3});
let unique = [];
let tempArr = [];
console.log('before', data);
data.forEach((value, index) => {
if (unique.indexOf(value.id) === -1) {
unique.push(value.id);
} else {
tempArr.push(index);
}
});
tempArr.reverse();
tempArr.forEach(ele => {
data.splice(ele, 1);
});
console.log(data);
Solution 73 - Javascript
The easiest way is to transform values into strings to filter also nested objects values.
const uniq = (arg = []) => {
const stringifyedArg = arg.map(value => JSON.stringify(value))
return arg.filter((value, index, self) => {
if (typeof value === 'object')
return stringifyedArg.indexOf(JSON.stringify(value)) === index
return self.indexOf(value) === index
})
}
console.log(uniq([21, 'twenty one', 21])) // [21, 'twenty one']
console.log(uniq([{ a: 21 }, { a: 'twenty one' }, { a: 21 }])) // [{a: 21}, {a: 'twenty one'}]
Solution 74 - Javascript
For my part this was the easiest solution
// A way to check if the arrays are equal
const a = ['A', 'B', 'C'].sort().toString()
const b = ['A', 'C', 'B'].sort().toString()
console.log(a === b); // true
// Test Case
const data = [ { group: 'A', name: 'SD' }, { group: 'B', name: 'FI' }, { group: 'A', name: 'SD' }, { group: 'B', name: 'CO' }];
// Return a new Array without dublocates
function unique(data) {
return data.reduce(function (accumulator, currentValue) {
// Convert to string in order to check if they are the same value.
const currentKeys = Object.keys(currentValue).sort().toString();
const currentValues = Object.values(currentValue).sort().toString();
let hasObject = false
for (const obj of accumulator) {
// Convert keys and values into strings so we can
// see if they are equal with the currentValue
const keys = Object.keys(obj).sort().toString();
const values = Object.values(obj).sort().toString();
// Check if keys and values are equal
if (keys === currentKeys && values === currentValues) {
hasObject = true
}
}
// Push the object if it does not exist already.
if (!hasObject) {
accumulator.push(currentValue)
}
return accumulator
}, []);
}
// Run Test Case
console.log(unique(data)); // [ { group: 'A', name: 'SD' }, { group: 'B', name: 'FI' }, { group: 'B', name: 'CO' } ]
Solution 75 - Javascript
Using mongoose I had an array of ObjectIds to work with.
I had a array/list of Object Ids to work with which first needed to be set to an string and after the unique set, amended back to Object Ids.
var mongoose = require('mongoose')
var ids = [ObjectId("1"), ObjectId("2"), ObjectId("3")]
var toStringIds = ids.map(e => '' + e)
let uniqueIds = [...new Set(toStringIds)]
uniqueIds = uniqueIds.map(b => mongoose.Types.ObjectId(b))
console.log("uniqueIds :", uniqueIds)
Solution 76 - Javascript
A modern approach that's extensible, fast, efficient and easy to read, using iter-ops library:
import {pipe, distinct} from 'iter-ops';
const input = [1, 1, 2, 2, 2, 3]; // our data
const i = pipe(input, distinct()); // distinct iterable
console.log([...i]); //=> [1, 2, 3]
And if your input is an array of objects, you will just provide a key selector for the distinct
operator.
Solution 77 - Javascript
Always remember, The build-in methods are easy to use. But keep in mind that they have a complexity.
The basic logic is best. There is no hidden complexity.
let list = [1, 1, 2, 100, 2] // your array
let check = {}
list = list.filter(item => {
if(!check[item]) {
check[item] = true
return true;
}
})
> or use, let check = [] if you need future traverse to checked items (waste of memory though)
Solution 78 - Javascript
Yet another answer, just because I wrote one for my specific use case. I happened to be sorting the array anyway, and given I'm sorting I can use that to deduplicate.
Note that my sort deals with my specific data types, you might need a different sort depending on what sort of elements you have.
var sortAndDedup = function(array) {
array.sort(function(a,b){
if(isNaN(a) && isNaN(b)) { return a > b ? 1 : (a < b ? -1 : 0); }
if(isNaN(a)) { return 1; }
if(isNaN(b)) { return -1; }
return a-b;
});
var newArray = [];
var len = array.length;
for(var i=0; i<len; i++){
if(i === 0 || array[i] != array[i-1]){
newArray.push(array[i]);
}
}
};
Solution 79 - Javascript
This script modify the array, filtering out duplicated values. It works with numbers and strings.
https://jsfiddle.net/qsdL6y5j/1/
Array.prototype.getUnique = function () {
var unique = this.filter(function (elem, pos) {
return this.indexOf(elem) == pos;
}.bind(this));
this.length = 0;
this.splice(0, 0, unique);
}
var duplicates = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
duplicates.getUnique();
alert(duplicates);
This version instead, allow you to return a new array with unique value keeping the original (just pass true).
https://jsfiddle.net/dj7qxyL7/
Array.prototype.getUnique = function (createArray) {
createArray = createArray === true ? true : false;
var temp = JSON.stringify(this);
temp = JSON.parse(temp);
if (createArray) {
var unique = temp.filter(function (elem, pos) {
return temp.indexOf(elem) == pos;
}.bind(this));
return unique;
}
else {
var unique = this.filter(function (elem, pos) {
return this.indexOf(elem) == pos;
}.bind(this));
this.length = 0;
this.splice(0, 0, unique);
}
}
var duplicates = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
console.log('++++ ovveride')
duplicates.getUnique();
console.log(duplicates);
console.log('++++ new array')
var duplicates2 = [0, 0, 1, 1, 2, 3, 1, 1, 0, 4, 4];
var unique = duplicates2.getUnique(true);
console.log(unique);
console.log('++++ original')
console.log(duplicates2);
Browser support:
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.5 (1.8) 9 (Yes) (Yes)
Solution 80 - Javascript
You can try this:
function removeDuplicates(arr){
var temp = arr.sort();
for(i = 0; i < temp.length; i++){
if(temp[i] == temp[i + 1]){
temp.splice(i,1);
i--;
}
}
return temp;
}
Solution 81 - Javascript
I would sort the array, then all duplicates are neighbours. Then walk once through the array and eliminate all duplicates.
function getUniques(array) {
var l = array.length
if(l > 1) {
// get a cloned copy and sort it
array = [...array].sort();
var i = 1, j = 0;
while(i < l) {
if(array[i] != array[j]) {
array[++j] = array[i];
}
i++;
}
array.length = j + 1;
}
return array;
}
Solution 82 - Javascript
Here is another approach using comparators (I care more about clean code than performance):
const list = [
{name: "Meier"},
{name: "Hans"},
{name: "Meier"},
]
const compare = (a, b) => a.name.localeCompare(b.name);
const uniqueNames = list.makeUnique(compare);
uniqueNames.pushIfAbsent({name: "Hans"}, compare);
Prototype declaration:
declare global {
interface Array<T> {
pushIfAbsent(item: T, compare:(a:T, b:T)=>number): number;
}
interface Array<T> {
makeUnique(compare:(a:T, b:T)=>number): Array<T>;
}
}
Array.prototype.pushIfAbsent = function <T>(this:T[], item:T, compare:(a:T, b:T)=>number) {
if (!this.find(existing => compare(existing, item)===0)) {
return this.push(item)
} else {
return this.length;
}
}
Array.prototype.makeUnique = function <T>(this:T[], compare:(a:T, b:T)=>number) {
return this.filter((existing, index, self) => self.findIndex(item => compare(existing, item) == 0) == index);
}
Solution 83 - Javascript
(function() {
"use strict";
Array.prototype.unique = function unique() {
var self = this;
return self.filter(function(a) {
var that = this;
// console.log(that);
return !that[a] ? that[a] = true : false;
}, {});
}
var sampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var distinctArray = sampleArray.unique();
console.log(distinctArray);
})();
Here is the simple way to solve this problem...
Solution 84 - Javascript
I used Array#reduce as way to create Array#unique
Array.prototype.unique = function() {
var object = this.reduce(function(h, v) {
h[v] = true;
return h;
}, {});
return Object.keys(object);
}
console.log(["a", "b", "c", "b", "c", "a", "b"].unique()); // => ["a", "b", "c"]