# How to find duplicates in an array using JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jul 3, 2021. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-array-find-duplicates

> Learn how to check if an array contains duplicate values using indexOf(), set object, and iteration in JavaScript. 

There are multiple methods available to check if an array contains duplicate values in JavaScript. You can use the `indexOf()` method, the `Set` object, or iteration to identify repeated items in an [array](https://attacomsian.com/blog/javascript-arrays).

## `Set` Object

[Set](https://attacomsian.com/blog/sets-javascript) is a special data structure introduced in ES6 that stores a collection of unique values. Since each value in a `Set` has to be unique, passing any duplicate item will be removed automatically:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const unique = Array.from(new Set(numbers));

console.log(unique);
// [ 1, 2, 3, 4, 5, 6 ]
```

The [Array.from()](https://attacomsian.com/blog/javascript-array-from) method, we used above, converts the `Set` back to an array. This is required because a `Set` is not an array. You could also use spread operator if you want for conversion:

```javascript
const unique = [...new Set(numbers)];
```

To **check if there were duplicate items** in the original array, just compare the length of both arrays:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const unique = Array.from(new Set(numbers));

if(numbers.length === unique.length) {
    console.log(`Array doesn't contain duplicates.`);
} else {
    console.log(`Array contains duplicates.`);
}
// Output: Array contains duplicates.
```

To **find out exactly which elements are duplicates**, you could make use of the `unique` array above, and remove each item from the original array as shown below:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const set = new Set(numbers);

const duplicates = numbers.filter(item => {
    if (set.has(item)) {
        set.delete(item);
    } else {
        return item;
    }
});

console.log(duplicates);
// [ 2, 5 ]
```

## `indexOf()` Method

In this method, we compare the index of the first occurrence of an element with all the elements in an array. If they do not match, it implies that the element is a duplicate:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

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

console.log(duplicates);
// [ 2, 5 ]
```

The above solution works perfectly as long as you only want to check if the array contains repeated items. However, the output array may contain duplicate items if those items occur more than twice in the array:

```javascript
const numbers = [1, 2, 3, 2, 2, 4, 5, 5, 6];

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

console.log(duplicates);
// [ 2, 2, 5 ]
```

## `some()` Method

In JavaScript, the `some()` method returns `true` if one or more elements pass a certain condition.

Just like the `filter()` method, the `some()` method iterates over all elements in an array to evaluate the given condition. 

In the callback function, we again use the `indexOf()` method to compare the current element index with other elements in the array. If both the indexes are the same, it means that the current item is not duplicate:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const isDuplicate = numbers.some((item, index) => index !== numbers.indexOf(item));

if (!isDuplicate) {
    console.log(`Array doesn't contain duplicates.`);
} else {
    console.log(`Array contains duplicates.`);
}
// Output: Array contains duplicates.
```

## `for` Loop

Finally, the last method to find duplicates in an array is to use the [`for` loop](https://attacomsian.com/blog/javascript-loops).

Here is an example that compares each element of the array with all other elements of the array to check if two values are the same using nested for loop:

```javascript
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

let isDuplicate = false;

// Outer for loop
for (let i = 0; i < numbers.length; i++) {
    // Inner for loop
    for (let j = 0; j < numbers.length; j++) {
        // Skip self comparison
        if (i !== j) {
            // Check for duplicate
            if (numbers[i] === numbers[j]) {
                isDuplicate = true;
                // Terminate inner loop
                break;
            }
        }
        // Terminate outer loop
        if (isDuplicate) {
            break;
        }
    }
}

if (!isDuplicate) {
    console.log(`Array doesn't contain duplicates.`);
} else {
    console.log(`Array contains duplicates.`);
}
// Output: Array contains duplicates.
```
