| var uniqueArray = function(arrArg) { | |
| return arrArg.filter(function(elem, pos,arr) { | |
| return arr.indexOf(elem) == pos; | |
| }); | |
| }; | |
| var uniqEs6 = (arrArg) => { | |
| return arrArg.filter((elem, pos, arr) => { | |
| return arr.indexOf(elem) == pos; | |
| }); | |
| } | |
| var test = ['mike','james','james','alex']; | |
| var testBis = ['alex', 'yuri', 'jabari']; | |
| console.log(uniqueArray(test.concat(testBis))); |
This comment has been minimized.
This comment has been minimized.
VonD
commented
Jul 15, 2016
|
You could also use the new Set([1, 2, 3, 1, 2, 3]).toJSON() // [1, 2, 3] |
This comment has been minimized.
This comment has been minimized.
guidobouman
commented
Jul 20, 2016
|
Or use the spread operator: [ ...new Set([1, 2, 3, 1, 2, 3]) ] // [1, 2, 3] |
This comment has been minimized.
This comment has been minimized.
jedrichards
commented
Oct 25, 2016
|
(+1 for |
This comment has been minimized.
This comment has been minimized.
augnustin
commented
Nov 23, 2016
|
|
This comment has been minimized.
This comment has been minimized.
idangozlan
commented
May 11, 2017
|
Thanks :) |
This comment has been minimized.
This comment has been minimized.
cup
commented
Jun 28, 2017
[11, 22, 22, 33].reduce((x, y) => x.includes(y) ? x : [...x, y], []) |
This comment has been minimized.
This comment has been minimized.
glococo
commented
Jul 2, 2017
•
|
Excelent shortest function and without map (above function). var some = [ {name:"Guille", last:"Foo"},{name:"Jorge", last:"bar"},{name:"Pedro", last:"Foo"},{name:"Guille", last:"Ipsum"} ]; |
This comment has been minimized.
This comment has been minimized.
domwashburn
commented
Jul 2, 2017
|
@svnpenn this is excellent! |
This comment has been minimized.
This comment has been minimized.
tjcafferkey
commented
Jul 11, 2017
|
Nice! Only thing missing really, there's no safety checks in place. What if your function is given a string, or an integer? You're assuming it's always going to be an array. |
This comment has been minimized.
This comment has been minimized.
4031651
commented
Jul 14, 2017
•
[1,2,2,3,1,4,1,4,2,3].filter((el, i, a) => i === a.indexOf(el)) |
This comment has been minimized.
This comment has been minimized.
shellscape
commented
Jul 27, 2017
|
+1 for |
This comment has been minimized.
This comment has been minimized.
FaiChou
commented
Sep 25, 2017
how to deal with this situation? |
This comment has been minimized.
This comment has been minimized.
LuisPaGarcia
commented
Oct 5, 2017
|
Thanks a lot for this! |
This comment has been minimized.
This comment has been minimized.
andr83
commented
Oct 14, 2017
|
Filter with indexOf has n2 complexity - do not think it's the best chose. Option with Set looking better. |
This comment has been minimized.
This comment has been minimized.
bilousov94
commented
Nov 7, 2017
|
Also +1 [ ...new Set([1, 2, 3, 1, 2, 3]) ] |
This comment has been minimized.
This comment has been minimized.
KarloZKvasin
commented
Nov 14, 2017
|
Set is shorter but filter with indexof is faster. example |
This comment has been minimized.
This comment has been minimized.
mordentware
commented
Nov 30, 2017
•
|
@KarloZKvasin: You can mutate the accumulator for the reducer if speed is your concern for the reduce. Building on your example, times for me were:
EDIT: Above was with Chrome (Firefox gave similar results, albeit a little higher). Hilariously, with IE11 on the same machine my times were:
IE Edge was a little better:
Basically, filter + indexof is probably the way to go if speed is your concern. |
This comment has been minimized.
This comment has been minimized.
spotsadmin
commented
Dec 21, 2017
|
If the array elements are objects, indexOf won't help. Here is a soln for that (in es6)
|
This comment has been minimized.
This comment has been minimized.
lionxcr
commented
Jan 6, 2018
•
|
This is what I came up with as I needed to filter results from an elastic search
will return a unique array filtered on any key...
Hope this can save others some time! |
This comment has been minimized.
This comment has been minimized.
timrsmith
commented
Feb 4, 2018
•
|
For generating a unique array of objects: uniqueArray = a => [...new Set(a.map(o => JSON.stringify(o)))].map(s => JSON.parse(s)) |
This comment has been minimized.
This comment has been minimized.
guillaumegarcia13
commented
Feb 20, 2018
|
@timrsmith Just a small word of caution with the JSON.parse(JSON.stringify(...)) approach. If you have properties with Date they will be replaced with their JSON representation (toISOString()).
|
This comment has been minimized.
This comment has been minimized.
spacehunter
commented
Feb 28, 2018
|
@timrsmith beautiful! |
This comment has been minimized.
This comment has been minimized.
wmhilton
commented
Mar 16, 2018
|
I too feel compelled to say: +1 for @guidobouman's |
This comment has been minimized.
This comment has been minimized.
rravithejareddy
commented
Mar 29, 2018
|
Check with include before add, like below Add() } Output: [1,2,3,4] |
This comment has been minimized.
This comment has been minimized.
xyzdata
commented
Mar 30, 2018
ES6 Set & Unique keyslet left = ["research", "stockfast01", "news", "stockfast01"];
left = [...new Set(left)];
https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array https://codereview.stackexchange.com/questions/60128/removing-duplicates-from-an-array-quickly |
This comment has been minimized.
This comment has been minimized.
whitehorse0
commented
Apr 11, 2018
•
|
ES6 version
|
This comment has been minimized.
This comment has been minimized.
pikislabis
commented
Apr 24, 2018
|
@timrsmith +1 |
This comment has been minimized.
This comment has been minimized.
karosi12
commented
Apr 25, 2018
•
|
var arr = [1,2,4,13,1]; |
This comment has been minimized.
This comment has been minimized.
seanmavley
commented
Jun 6, 2018
|
So how does this work when merging should happen using a property of the array object? |
This comment has been minimized.
This comment has been minimized.
iamvanja
commented
Jun 19, 2018
|
@seanmavley You can merge array before passing it to const a = [1, 2, 3]
cont b = [2, 3, 4, 5]
const uniqueMerged = [...new Set([...a, ...b])] // 1, 2, 3, 4, 5 |
This comment has been minimized.
This comment has been minimized.
VSmain
commented
Jul 13, 2018
•
|
do it with ladosh
|
This comment has been minimized.
This comment has been minimized.
macmladen
commented
Aug 15, 2018
|
@mordentware I was puzzled with your results so I made a CodePen with my own data sample where I needed to remove duplicates. Data array has 6.288 items, 5.284 of them are unique. Results are nearly the same for both sorted and unsorted arrays. My findings are that filter and reduce are similar while Set was much faster. Reduce with spread was much slower due to a large number of deconstruction/construction. See the Pen Deduplicating speed test by Mladen Đurić (@macmladen) on CodePen. (times may vary due to system, browser, CPU, memory...) Results on MacBook Pro i7 form 2011, using Firefox (usually with few hundred open tabs ;) |
This comment has been minimized.
This comment has been minimized.
joeyparrish
commented
Sep 6, 2018
•
|
Just discovered this thread and found that
|
This comment has been minimized.
This comment has been minimized.
nabilfreeman
commented
Sep 12, 2018
|
Set is so cool! Never knew it existed! |
This comment has been minimized.
This comment has been minimized.
joshuapinter
commented
Oct 18, 2018
This comment has been minimized.
This comment has been minimized.
impfromliga
commented
Nov 12, 2018
it's just because the better complexity will faster on bigger counts. And be faster greatly be on them, instead of 10 elements array... |
This comment has been minimized.
This comment has been minimized.
brunoandradebr
commented
Jan 19, 2019
|
If you need to filter by object value and a performance way : // creates an object only once - garbage be glad ^^
let cachedObject = {};
// array to be filtered
let arr = [
{id : 0, prop : 'blah'},
{id : 1, prop : 'foo'},
{id : 0, prop : 'bar'}
]
// "filter" to object - keep original array - garbage be glad too ^^
arr.map((item)=> cachedObject[item.id] = item)
// optional, turns object to array
arr = Object.values(cachedObject)
|
This comment has been minimized.
This comment has been minimized.
Kr3m
commented
Feb 8, 2019
•
|
What's wrong with
Surely this is a lot simpler if you're just trying to remove duplicates. Obviously this would work better as the return statement in a function. I'm just posting this as an example. |
This comment has been minimized.
This comment has been minimized.
little-brother
commented
Feb 13, 2019
|
This comment has been minimized.
This comment has been minimized.
philihp
commented
Feb 26, 2019
|
I find this reads a little bit better if you stash your reducer function in a named variable. const duplicates = (e, i, arr) => arr.indexOf(e) === i
let arr = [1, 2, 3, 4, 3, 2];
arr.filter(duplicates); |
This comment has been minimized.
This comment has been minimized.
indatawetrust
commented
Mar 15, 2019
•
Array.prototype.uniq = function(key) {
return key
? this.map(e => e[key])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter(e => this[e])
.map(e => this[e])
: [...new Set(this)];
} |
This comment has been minimized.
This comment has been minimized.
patrickmichalina
commented
Mar 16, 2019
•
|
In Typescript export const dedupeByProperty =
<T>(arr: ReadonlyArray<T>, objKey: keyof T) =>
arr.reduce<ReadonlyArray<T>>((acc, curr) =>
acc.some(a => a[objKey] === curr[objKey])
? acc
: [...acc, curr], []) |
This comment has been minimized.
This comment has been minimized.
pankajkrr
commented
Jul 27, 2019
|
Find brief article here : Click here to view |

This comment has been minimized.
mbplautz commentedApr 1, 2016
Rock on, this code is exactly what I was looking for. For an unnecessarily cryptic version of
uniqEs6, you could instead use the one-liner:var uniqEs6 = (arrArg) => arrArg.filter((elem, pos, arr) => arr.indexOf(elem) == pos);(Of course there are those JavaScripters who believe that
(...) => {}is not intended to be a replacement forfunction(...) {})