DEV Community

Cover image for 3 ways to remove duplicates in an Array in Javascript

3 ways to remove duplicates in an Array in Javascript

Lenin Felix on September 07, 2021

Let's check, many times (or few) arises the need to remove duplicate elements given in an array, I don't know... it can be because you have to prin...
Collapse
 
sash20m profile image
Alex Matei

The first one is sexier

Collapse
 
eaallen profile image
Elijah Allen

The last two are problematic because you are essentially calling a for loop in a for loop which heavily increases how long the algorithms are going to take.

Using a set to remove duplicates is a great to solve this problem.

Collapse
 
jimjam88 profile image
James Morgan

Don't forget reduce!

chars.reduce((acc, char) => acc.includes(char) ? acc : [...acc, char], []);

Collapse
 
spock123 profile image
Lars Rye Jeppesen

This is my preferred way,. I don't like using Sets

Collapse
 
devman profile image
DevMan • Edited

Complexities are:

  1. O(N)

2 and 3 are O(N^2)

So 1 should always be used imho.

Collapse
 
andrewjhart profile image
Andrew Hart • Edited

This is great for most situations, using a Map -- my personal testing has found that arrays up to a certain length still perform better with reduce, etc than Maps, but beyond N values (which I can't recall the exact amount and I'm sure it varies on the types), Map's absolutely crush them because the op is O(n), as noted by DevMan -- just thought it was worth noting.

Collapse
 
jasimalrawie profile image
JasimAlrawie

Or
let chars = ['A', 'B', 'A', 'C', 'B'];

let uniqueChars = [];
chars.forEach((e) => {
if (!(e in chars)) {
uniqueChars.push(e);
}
});

console.log(uniqueChars);

Collapse
 
rowild profile image
Robert Wildling

Shouldn't that be if (!(e in uniqueChars)) { ?

Collapse
 
phibya profile image
Phi Byă

For big array of objects, use reduce and Map:

[{a: 1, b: 2}, {a: 2, b: 3}, {a: 1, b: 2}].reduce((p, c) => p.set(c.a, c), new Map()).values()

Collapse
 
vedranbasic profile image
Vedran-Basic

Imagine having array of objects with n props having only to remove duplicate props with m same properties where m < n and use first of the two or more duplicates of the same unique constraint rule.

That's where science begins. Would be much more interested in hearing different solutions to this topic.

Collapse
 
monello profile image
Morné Louw

Then write your own article an stop trying to sound smart on someone else's post

Collapse
 
phibya profile image
Phi Byă • Edited

Simply use reduce and Map:

[{a: 1, b: 2, c: 3}, {a: 2, b: 3, c: 4}, {a: 1, b: 2, c: 5}].reduce((p, c) => p.set([c.a, c.b].join('|'), c), new Map()).values()

Edit: For selecting the first value, use Map.has before setting the value.

Collapse
 
tutrinh profile image
Tu Trinh

Thanks for the article. This is useful.

Collapse
 
tohodo profile image
Tommy

What is an efficient way to perform this operation "in place"?

Collapse
 
lucianodouglasm2 profile image
Luciano Douglas Machado Chagas

I usually do it using Define, it seems to be more performative. Could you show the performance of each of these modalities?

Collapse
 
antonvryckij profile image
Anton Vryckij

Thank you for the article!

Collapse
 
nahid570 profile image
Nahid Faraji

I wanna remove from array of object where multiple object contain same id but I wanna remove only first one. How?