How to find duplicates in an array - JavaScript

Mo Ibra

Mo Ibra

Dec 24, 2022

In this article we will learn how to find duplicates in an array.

It is very common to find duplicates in an array in JavaScript.


Problem

Now we have a problem, which is that we want to find the duplicate elements in the array.

Imagine we have an array like this:

[1, 2, 3, 3, 4, 5, 5];

And we want to find duplicates, so the output will be:

[3, 5]

How to solve this problem?

Well this problem is easy to solve, all we have to do is iterate over the array and if we find a duplicates element we add it in a new array.

The solution will be as follows:

  • Create new array called unique.
  • Iterate over the array elements.
  • Push elements to new array (unqiue).
  • If element already in new array push it to another array called duplicates.

Find duplicates elements in an array

// Array
const array = [1, 2, 3, 3, 4, 5, 5];

// Duplicate elements will be here.
const duplicates = [];

// Unique elements will be here.
const unique = [];

// Iterate over elements
array.forEach(ele => {
    
    if (unique.includes(ele)) {
        duplicates.push(ele);
    } else {
        unique.push(ele);
    }
});

// Result:
console.log(duplicates);

Output

[ 3, 5 ]

There is another solution that is easier than the first solution, and it is through Set


Find duplicates elements using Set

Set is a special data structure introduced in ES6 that stores a collection of unique values.

As long as the set accepts unique elements only, this means that if we add an array with duplicate elements, it will remove them automatically.

Example

// Array
const array = [1, 2, 3, 3, 4, 5, 5];

// Set
const set = Array.from(new Set(array));

// Print Array
console.log(array);

// Print Set
console.log(set);

Output

[
  1, 2, 3, 3,
  4, 5, 5
]
[ 1, 2, 3, 4, 5 ]

As you can see, Set has removed duplicate elements automatically.

From here we can make a condition, if the array and the set are equal in length, this means that there are no duplicate elements (nothing has been removed), and if they are not equal, this means that there are duplicate elements.

// Array
const array = [1, 2, 3, 3, 4, 5, 5];

// Set
const set = Array.from(new Set(array));

if (array.length === set.length) {
    console.log('No duplicate elements');
} else {
    console.log('Duplicate elements')
}

Output

Duplicate elements

And if you want to know exactly what are the duplicates elements, as we did in the first function.

// Array
const array = [1, 2, 3, 3, 4, 5, 5];

// Set
const set = new Set(array);

// Find duplicates
const duplicates = array.filter(ele => {
    if (set.has(ele)) {
        set.delete(ele);
    } else {
        return ele;
    }
});

// Result:
console.log(duplicates);

Output

[3, 5]

Find duplicates using indexOf method

We can use indexOf method to solve this problem as well.

Example

// Array
const array = [1, 2, 3, 3, 4, 5, 5];

// Find duplicates
const duplicates = array.filter((item, index) => index !== numbers.indexOf(item));

// Result:
console.log(duplicates);

Output

[ 3, 5 ]

Thank you for reading

Thank you for reading my blog. 🚀

Suggestions For More Articles: