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 print a list from the super, remove a student that duplicated his record in a form, infinity of things, so let's see some ways to do this:
1) Use Set
Using Set(), an instance of unique values will be created, implicitly using this instance will delete the duplicates.
So we can make use of this instance and from there we will have to convert that instance into a new array, and that would be it:
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);
Output:
['A', 'B', 'C']
2) Using the indexOf() and filter() methods
The indexOf() method returns the index of the first occurrence of the element in the array:
let chars = ['A', 'B', 'A', 'C', 'B'];
chars.indexOf('B');
Output:
1
The duplicate element is the element whose index is different from its indexOf() value:
let chars = ['A', 'B', 'A', 'C', 'B'];
chars.forEach((element, index) => {
console.log(`${element} - ${index} - ${chars.indexOf(element)}`);
});
Output:
A - 0 - 0
B - 1 - 1
A - 2 - 0
C - 3 - 3
B - 4 - 1
To eliminate duplicates, the filter() method is used to include only the elements whose indexes match their indexOf values, since we know that the filer method returns a new array based on the operations performed on it:
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = chars.filter((element, index) => {
return chars.indexOf(element) === index;
});
console.log(uniqueChars);
Output:
['A', 'B', 'C']
And if by chance we need the duplicates, we can modify our function a little, just by changing our rule:
let chars = ['A', 'B', 'A', 'C', 'B'];
let dupChars = chars.filter((element, index) => {
return chars.indexOf(element) !== index;
});
console.log(dupChars);
Output:
['A', 'B']
3) Using the includes() and forEach() methods
The include() function returns true if an element is in an array or false if it is not.
The following example iterates over the elements of an array and adds to a new array only the elements that are not already there:
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [];
chars.forEach((element) => {
if (!uniqueChars.includes(element)) {
uniqueChars.push(element);
}
});
console.log(uniqueChars);
Output:
['A', 'B', 'C']
Basically, we have options to solve this type of problem, so don't get stuck anymore and you can use whichever one appeals to you.
Top comments (17)
The first one is sexier
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.
Don't forget reduce!
chars.reduce((acc, char) => acc.includes(char) ? acc : [...acc, char], []);
This is my preferred way,. I don't like using Sets
Complexities are:
2 and 3 are O(N^2)
So 1 should always be used imho.
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.
Or
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [];
chars.forEach((e) => {
if (!(e in chars)) {
uniqueChars.push(e);
}
});
console.log(uniqueChars);
Shouldn't that be
if (!(e in uniqueChars)) {
?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()
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.
Then write your own article an stop trying to sound smart on someone else's post
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.
Thanks for the article. This is useful.
What is an efficient way to perform this operation "in place"?
I usually do it using Define, it seems to be more performative. Could you show the performance of each of these modalities?
Thank you for the article!
I wanna remove from array of object where multiple object contain same id but I wanna remove only first one. How?