JavaScript is much simpler than, any other language. This article will help you to check for duplicates in Array – JavaScript.

4 Ways to Check for duplicates in Array JavaScript

There are many ways we can use to check duplicates values in an Array. We will see some of the easiest ways and finally built our own logic to achieve the same.

Array.filter() Method

This method is really handy when it comes to filtering an array based on certain conditions. If you want to learn more about Array.filter() method then, we have already posted an in-depth explanation of  Array.filter method in JavaScript.

Further, we can use it along with the indexOf() method to check for duplicates in an Array.

let numArray = [1,2,3,3,4,3,1,2,6,7,0,9];

let duplicates = numArray.filter( (val, index) => 
                index !== numArray.indexOf(val));

  console.log('No duplicate found')
  console.log('Duplicates found: ' + duplicates); //3,3,1,2

some() Method

The some() method is the same as that of Array.filter() method as shown in the above example.

The only difference is that it returns a boolean ( true or false) after evaluation.
Whereas, the Array.filter() method return an array after evaluation.

Thus, we can also check for duplicates using some() method in JavaScript.

let numArray = [1,2,3,3,4,3,1,2,6,7,0,9];

let duplicates = numArray.some( (val, index) => 
                index !== numArray.indexOf(val));

  console.log('Duplicates found'); //Duplicates found
  console.log('No duplicate found');

JavaScript Collection Set() Class

Set class is used to have a unique collection of values. To use this class we need to create its object/instance. And simply pass the given array which contains the duplicates

As the Class is built-in, we don’t need to worry about anything. To get the unique value you must change the object back to an Array.

Note: Here, we assume that the array contains the duplicate. And we need another array containing the unique values.

let numArray = [1,2,3,3,4,3,1,2,6,7,0,9];

let obj = new Set(numArray);
let uniqueArray = Array.from(obj);

console.log(uniqueArray); //[1,2,3,4,6,7,0,9]

 For-loop & if-else to Check Duplicates in an Array- JavaScript

Finally, we have created a javascript program to check duplicates values in an array using for loops and if-else.

The main objective of the below program is to illustrate that you can build your own logic without using in-built functions.

const numArray= [1,2,3,3,4,3,1,2,6,7,0,9];

let array = numArray;  //making a copy of orignal array
let temp = 0;          //temperory variable to swap array vals

// first sorting array in ascending order
for(let i=0;i<array.length;i++){
    for(let j=i+1;j<array.length;j++){
        temp = array[i];
         if(array[i] < array[j]){
            array[i] = array[j];
            array[j] = temp;


let uniqueValues = [];
let duplicateValues = [];
let count = 0;   //count duplicates
let totalNum=0;  //count unique vals
let index=0;  

// check for duplicate values
for(let i=0;i<array.length;i++){
    for(let j=i+1;j<array.length;j++){ 
           duplicateValues[index]=array[i+count]; //store duplicate val
uniqueValues[totalNum]=array[i]; //store only unique val 
        i=i+count; //move i to the unique element after a repeted val
        count = 0; //reset the count

// display if duplicates are found
if(numArray.length == uniqueValues.length){
    console.log("Array contains no duplicates");
    console.log("Array contains duplicates\n");  
    console.log("Duplicate value are: "+ duplicateValues); //3,3,2,1

In conclusion, we hope this article helped you to check for duplicates in an Array.

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed