Check If An Array Contains Duplicates In JavaScript

To check if an array contains duplicates in JavaScript, you can use any of these methods –

  1. Pass the array to the Set constructor and check if the size of the Set is equal to the length of the array. If not, the array contains duplicates.
  2. Use the some() method and check if there is at least one element whose first occurrence is not equal to its last occurrence using the indexOf() method. If there is an element like that, the array contains duplicates.

Let’s discuss each of these methods in detail below.

how to check if an array contains duplicates in JavaScript

Use Set() To Check If An Array Contains Duplicates In JavaScript

The Set object lets you store unique values of any type, whether primitive values or object references.

When you pass an array to the Set constructor, it removes any duplicate elements. So, if the size of the Set is not equal to the length of the array, then it contains duplicates.

For example:

let arr = [1,2,1,3,4];

var set = new Set(arr);

var size = set.size;

if(arr.length!= size) {

  console.log("Array contains duplicates");

}else{

  console.log("Array doesn't contain duplicates");

}

Output: Array contains duplicates

In the above code, we have created a Set from the given array. Then we have checked if the size of the Set is equal to the length of the array or not.

If the size of the set is not equal to the length of the array, it means that there are duplicate elements in the array.

Use some() To Check If An Array Contains Duplicates In JavaScript

The some() method executes the callback function once for each element present in the array until it finds an element where the callback returns a truthy value.

If such an element is found, some() returns true and doesn’t check the remaining values. Otherwise, it returns false.

We can use the some() method along with indexOf() to check if an array contains duplicates or not as shown below:

let arr = [1,2,1,3,4];

var isDuplicate = arr.some(element => {

  return arr.indexOf(element) != arr.lastIndexOf(element);

});

console.log(isDuplicate); //true

In the above code, we have passed a callback function to the some() method. The callback function checks if there is an element in the array whose first occurrence is not equal to its last occurrence. This would be a duplicate element, because otherwise it’s first index should match it’s last index, i.e., the element should be present at only one index in the array.

If such an element is found, it returns true and the loop doesn’t execute further. Otherwise, it returns false.

Let’s see another example:

let arr = ['banana','apple','orange'];

var isDuplicate = arr.some(element => {

  return arr.indexOf(element) !=arr.lastIndexOf(element);

});

console.log(isDuplicate); //false

In the above code, there are no duplicate elements in the array, so the some() method returns false.

Conclusion

In this article, you learned about how to check if an array contains duplicates in JavaScript. You can use any of the above methods depending on your requirements.

I hope this helps. Thanks for reading! 🙂

Leave a Reply