DEV Community

Tiberius Mairura
Tiberius Mairura

Posted on • Edited on

Remove Duplicates From Array

Assume we have a list of numbers that looks like this:

[1, 2, 4, 5, 4, 2];
Enter fullscreen mode Exit fullscreen mode

How can we remove the duplicates?

Image description

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 ]
Enter fullscreen mode Exit fullscreen mode

What are we doing here?

  1. We have declared a numbersWithoutDuplicates and assigned to an empty array. This variable will finally hold our numbers after removing duplicates

  2. We are iterating through all the numbers using a for-loop

  3. For each iteration, we are checking if the current number is in our numbersWithoutDuplicates array.

  4. If the current number is in the array, we do nothing, otherwise we add the number

  5. 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 ]
Enter fullscreen mode Exit fullscreen mode

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 ]
Enter fullscreen mode Exit fullscreen mode

Yaahs!

Image description
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)

Collapse
 
rajeshroyal profile image
Rajesh Royal

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.

Collapse
 
buzzdee profile image
Sebastian Schlatow • Edited

How do you remove duplicates from an array with non primitive types? (Without a third party library.)

Collapse
 
rajeshroyal profile image
Rajesh Royal

use a HashMap or you can use Object.

Collapse
 
ovindu profile image
Ovindu Imbulgasmulle • Edited

Take a look at how these guys doing and build your own lodash.com/docs/#uniqBy.

Edited the comment since you did the same

Collapse
 
buzzdee profile image
Sebastian Schlatow

Without a third party library.

Thread Thread
 
ovindu profile image
Ovindu Imbulgasmulle

If you think about it, you can use for loop, some and includes to build a logic for that...

Collapse
 
frankwisniewski profile image
Frank Wisniewski

let numbersWithoutDuplicates = [...new Set(numbers)]

Collapse
 
hermitex profile image
Tiberius Mairura

Fixed. Thanks!

Collapse
 
detzam profile image
webstuff

good one