I need to check a JavaScript array to see if there are any duplicate values. What's the easiest way to do this? I just need to find what the duplicated values are - I don't actually need their indexes or how many times they are duplicated.

I know I can loop through the array and check all the other values for a match, but it seems like there should be an easier way. Any ideas? Thanks!

Related: Remove Duplicates from JavaScript Array

share
11  
There seems to be years of confusion about what this question asks. I needed to know what elements in the array were duplicated: "I just need to find what the duplicated values are". The correct answer should NOT remove duplicates from the array. That's the inverse of what I wanted: a list of the duplicates, not a list of unique elements. – Scott Saunders Feb 22 '13 at 15:47

45 Answers 45

/* The indexOf method of the Array object is useful for comparing array items. IE is the only major browser that does not natively support it, but it is easy to implement: */

Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
    i= i || 0;
    var L= this.length;
    while(i<L){
        if(this[i]=== what) return i;
        ++i;
    }
    return -1;
}

function getarrayduplicates(arg){
    var itm, A= arg.slice(0, arg.length), dups= [];
    while(A.length){
        itm= A.shift();
        if(A.indexOf(itm)!= -1 && dups.indexOf(itm)== -1){
            dups[dups.length]= itm;
        }
    }
    return dups;
}

var a1= [1, 22, 3, 2, 2, 3, 3, 4, 1, 22, 7, 8, 9];

alert(getarrayduplicates(a1));

For very large arrays, it can be faster to remove the duplicates from the array as they are found, so that they will not be looked at again:

function getarrayduplicates(arg){
    var itm, A= arg.slice(0, arg.length), dups= [];
    while(A.length){
        itm= A.shift();
        if(A.indexOf(itm)!= -1){
            dups[dups.length]= itm;
            while(A.indexOf(itm)!= -1){
                A.splice(A.indexOf(itm), 1);
            }
        }
    }
    return dups;
}
share

From Raphael Montanaro answer, it can improve to use with array/object item as follows.

function eliminateDuplicates(arr) {
  var len = arr.length,
      out = [],
      obj = {};

  for (var key, i=0; i < len; i++) {
    key = JSON.stringify(arr[i]);
    obj[key] = (obj[key]) ? obj[key] + 1 : 1;
  }
  for (var key in obj) {
    out.push(JSON.parse(key));
  }
  return [out, obj];
}

Note: You need to use JSON library for browser that's not supported JSON.

share

http://jsfiddle.net/vol7ron/gfJ28/

var arr  = ['hello','goodbye','foo','hello','foo','bar',1,2,3,4,5,6,7,8,9,0,1,2,3];
var hash = [];

// build hash
for (var n=arr.length; n--; ){
   if (typeof hash[arr[n]] === 'undefined') hash[arr[n]] = [];
   hash[arr[n]].push(n);
}


// work with compiled hash (not necessary)
var duplicates = [];
for (var key in hash){
    if (hash.hasOwnProperty(key) && hash[key].length > 1){
        duplicates.push(key);
    }
}    
alert(duplicates);
  1. The result will be the hash array, which will contain both a unique set of values and the position of those values. So if there are 2 or more positions, we can determine that the value has a duplicate. Thus, every place hash[<value>].length > 1, signifies a duplicate.

  2. hash['hello'] will return [0,3] because 'hello' was found in node 0 and 3 in arr[].

    Note: the length of [0,3] is what's used to determine if it was a duplicate.

  3. Using for(var key in hash){ if (hash.hasOwnProperty(key)){ alert(key); } } will alert each unique value.

share

I am trying to improve the answer from @swilliams, this will return an array without duplicates.

// arrays for testing
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];

// ascending order
var sorted_arr = arr.sort(function(a,b){return a-b;}); 

var arr_length = arr.length;
var results = [];
if(arr_length){
    if(arr_length == 1){
        results = arr;
    }else{
        for (var i = 0; i < arr.length - 1; i++) {
            if (sorted_arr[i + 1] != sorted_arr[i]) {
                results.push(sorted_arr[i]);
            }
            // for last element
            if (i == arr.length - 2){
                results.push(sorted_arr[i+1]);
            }
        }
    }
}

alert(results);
share

Here is one implemented using sort() and JSON.stringify()

https://gist.github.com/korczis/7598657

function removeDuplicates(vals) {
    var res = [];
    var tmp = vals.sort();

    for (var i = 0; i < tmp.length; i++) {
        res.push(tmp[i]);
                    while (JSON.stringify(tmp[i]) == JSON.stringify(tmp[i + 1])) {
            i++;
        }
    }

    return res;
}
console.log(removeDuplicates([1,2,3,4,5,4,3,3,2,1,]));
share
    
Why the downvote? – korCZis Nov 29 '13 at 16:08

Here is the one of methods to avoid duplicates into javascript array...and it supports for strings and numbers...

 var unique = function(origArr) {
    var newArray = [],
        origLen = origArr.length,
        found,
        x = 0; y = 0;

    for ( x = 0; x < origLen; x++ ) {
        found = undefined;
        for ( y = 0; y < newArray.length; y++ ) {
            if ( origArr[x] === newArray[y] ) found = true;
        }
        if ( !found) newArray.push( origArr[x] );    
    }
   return newArray;
}

check this fiddle..

share
    
Not what was asked for. – RWC Dec 3 '14 at 13:56

here is a small simple snippet to find unique and duplicate values with out sorting and two loops.

var _unique = function (arr) {
    var h = [], t = [];
    arr.forEach(function (n) {
        if (h.indexOf(n) == -1)
            h.push(n);
        else t.push(n);
    });
    return [h, t];
}
var result = _unique(["test",1,4,2,34,6,21,3,4,"test","prince","th",34]);
console.log("Has duplicate values : " + (result[1].length > 0))  //and you can check count of duplicate values
console.log(result[0]) //unique values
console.log(result[1]) //duplicate values

share

You can use the following construction:

var arr = [1,2,3,4,5,6,7,8,9,0,5];
var duplicate = arr.filter(function(item, i, arr) {
  return -1 !== arr.indexOf(item, i + 1);
})
share

Here's one without using a temp Array to store the non-duplicate ones:

// simple duplicate removal for non-object types
Array.prototype.removeSimpleDupes = function() {
    var i, cntr = 0, arr = this, len = arr.length;

    var uniqueVal = function(val,n,len) { // remove duplicates
        var dupe = false;
            for (i = n; i < len; i++) { 
                if (typeof arr[i]!=="undefined" && val===arr[i]) { arr.splice(i,1); dupe = true; }
            }
        return (dupe) ? arr.length : len;
    };

    while (cntr < len) {
        len = uniqueVal(arr[cntr],cntr+1,len);
        cntr++;
    }

    return arr;
};
share
function remove_dups(arrayName){
  var newArray = new Array();

  label:for(var i=0; i<arrayName.length; i++ ){  

     for(var j=0; j<newArray.length;j++ ){
       if(newArray[j]==arrayName[i]){
         continue label;
       }
     }

     newArray[newArray.length] = arrayName[i];

  }

  return newArray;
}
share
    
-1: Doesn't answer the question asked, which was to identify dupes, not remove them. You should probably also include some text describing your solution. – The DIMM Reaper Aug 20 '15 at 15:37

Surprised no one posted this solution.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>
</title>
</head>
<body>
  <script>
       var list = [100,33,45,54,9,12,80,100];
       var newObj = {};
       var newArr = [];
        for(var i=0; i<list.length; i++){
          newObj[list[i]] = i;               
        }
        for(var j in newObj){
            newArr.push(j);  
        }
       console.log(newArr);
  </script>
</body>
</html>
share
    
This doesn't do what the question asks. – Scott Saunders Jan 21 '14 at 18:21
    
@ScottSaunders: Easiest way to find duplicate values in a JavaScript array – Thalaivar Jan 21 '14 at 19:48
    
Does this show the duplicates or unique values? – Scott Saunders Jan 21 '14 at 20:38
    
@ScottSaunders: It would show you the unique values. – Thalaivar Jan 21 '14 at 20:57
2  
The question is asking how to show the duplicate values, not the unique values. – Scott Saunders Jan 21 '14 at 21:27

var arr = [4,5,1,1,2,3,4,4,7,5,2,6,10,9];
var sorted_arr = arr.sort();
var len = arr.length;
var results = [];
for (var i = 0; i < len; i++) {
  if (sorted_arr[i + 1] !== sorted_arr[i]) {
    results.push(sorted_arr[i]);
  }
}
document.write(results);

share
    
This does not do what the question asks. The loop runs one step too far. This answer is substantially the same as other older answers. – Scott Saunders Dec 8 '14 at 17:06
var a= [1, 2,2,3,3,4,4,4];
var m=[];
var n = [];
a.forEach(function(e) {
  if(m.indexOf(e)=== -1) {
    m.push(e);
}else if(n.indexOf(e)=== -1){
    n.push(e);
}

});
share
    
Although this code might solve the problem, one should always consider adding an explanation to it. – BDL Jan 20 at 14:00
    
Iterating array and storing the unique values in array M and duplicate values in n – santhosh Jan 21 at 14:18

In this post was useful for duplication check if u are using Jquery.

How to find the duplicates in an array using jquery

var unique_values = {}; var list_of_values = []; $('input[name$="recordset"]').     each(function(item) {          if ( ! unique_values[item.value] ) {             unique_values[item.value] = true;             list_of_values.push(item.value);         } else {             // We have duplicate values!         }     });
share
//program to find the duplicate elements in arraylist

import java.util.ArrayList;
import java.util.Scanner;

public class DistinctEle 
{ 
    public static void main(String args[])
    {
        System.out.println("Enter elements");
        ArrayList<Integer> abc=new ArrayList<Integer>();
        ArrayList<Integer> ab=new ArrayList<Integer>();
        Scanner a=new Scanner(System.in);
        int b;
        for(int i=0;i<=10;i++)
        {
            b=a.nextInt();
            if(!abc.contains(b))
            {
                abc.add(b);
            }
            else
            {
                System.out.println("duplicate elements"+b);
            }
        }
    }
}
share
2  
Consider explaining the code you've offered in your answer. – J0e3gan Sep 9 '15 at 17:05
3  
The question was regarding JavaScript not Java. – Nej Kutcharian Oct 12 '15 at 23:55

protected by baao Feb 28 '16 at 10:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.