Assume we have a list of numbers that looks like this:
[1, 2, 4, 5, 4, 2];
How can we remove the duplicates?
In this short tutorial, we are going to learn two methods that we can use to remove duplicates from an array.
1. Use a for-loop
?
let numbers = [1, 2, 4, 5, 4, 2];
let numbersWithoutDuplicates = [];
for (let i = 0; i < numbers.length; i++) {
if (!numbersWithoutDuplicates.includes(numbers[i])) {
numbersWithoutDuplicates.push(numbers[i]);
}
}
console.log(numbersWithoutDuplicates);
// => [ 1, 2, 4, 5 ]
What are we doing here?
We have declared a
numbersWithoutDuplicates
and assigned to an empty array. This variable will finally hold our numbers after removing duplicatesWe are iterating through all the numbers using a for-loop
For each iteration, we are checking if the current number is in our
numbersWithoutDuplicates
array.If the current number is in the array, we do nothing, otherwise we add the number
Finally, we are printing out our non-duplicate array
The for-loop can be re-written much more succinctly using a for-of
loop as below:
let numbers = [1, 2, 4, 5, 4, 2];
let numbersWithoutDuplicates = [];
for (let number of numbers) {
if (!numbersWithoutDuplicates.includes(number) {
numbersWithoutDuplicates.push(number);
}
}
console.log(numbersWithoutDuplicates);
// => [ 1, 2, 4, 5 ]
2. Use Set?
We can use a set to automatically exclude duplicates from our array.
Mathematically, a set is a collection of items where order and repetition is ignored.
In JavaScript we can create a set out of an array object and this will simply eliminate the duplicates.
Let us see how this works:
let numbers = [1, 2, 4, 5, 4, 2];
let numbersWithoutDuplicates = [...new Set(numbers)];
// populate a new array with non-duplicate numbers
// using the spread operator
console.log(numbersWithoutDuplicates);
// => [ 1, 2, 4, 5 ]
Yaahs!
We just learned two methods on how to remove duplicates from an array.
Which other cool ways do you know how to remove duplicates from arrays?
Top comments (9)
Without using any third party library you can use Map() [HashMap] in JS. What you have to do is keep track of already appeared numbers and push only those numbers in the
newArray
which are not already present in hashMap.How do you remove duplicates from an array with non primitive types? (Without a third party library.)
use a
HashMap
or you can use Object.Take a look at how these guys doing and build your own lodash.com/docs/#uniqBy.
Edited the comment since you did the same
Without a third party library.
If you think about it, you can use for loop, some and includes to build a logic for that...
let numbersWithoutDuplicates = [...new Set(numbers)]
Fixed. Thanks!
good one