In Javascript, how do I check if an array has duplicate values?
Asked Answered
C

13

164

Possible Duplicate:
Easiest way to find duplicate values in a javascript array

How do I check if an array has duplicate values?

If some elements in the array are the same, then return true. Otherwise, return false.

['hello','goodbye','hey'] //return false because no duplicates exist
['hello','goodbye','hello'] // return true because duplicates exist

Notice I don't care about finding the duplication, only want Boolean result whether arrays contains duplications.

Clearly answered 11/9, 2011 at 6:4 Comment(8)
Here it is: #841281Krysta
I don't want a list of duplicates removed. I just want to know true or false if a list has duplicates in it.Clearly
The accepted answer for the exact same question you asked is your answer. #841281Meenen
jsfiddle.net/vol7ron/gfJ28Kb
This question is not a duplicate. Since @Clearly simply wants to check if duplicates exists, the solution is faster/easier than what's needed to find all occurrences of duplicates. For example, you can do this: codr.io/v/bvzxhqmLesson
using underscore ,simple technique var test=['hello','goodbye','hello'] ; if ( test.length != _.unique(test).length ) { // some code }Gusman
Not a duplicate of the marked question. Please pay attention before marking questions as such.Superfecundation
Using underscore.js it is simple use if(arrayA.length === _.uniq(ArrayA).length){ // do your work} --> if true all are distinct else not distinctShama
C
336

If you have an ES2015 environment (as of this writing: io.js, IE11, Chrome, Firefox, WebKit nightly), then the following will work, and will be fast (viz. O(n)):

function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}

If you only need string values in the array, the following will work:

function hasDuplicates(array) {
    var valuesSoFar = Object.create(null);
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (value in valuesSoFar) {
            return true;
        }
        valuesSoFar[value] = true;
    }
    return false;
}

We use a "hash table" valuesSoFar whose keys are the values we've seen in the array so far. We do a lookup using in to see if that value has been spotted already; if so, we bail out of the loop and return true.


If you need a function that works for more than just string values, the following will work, but isn't as performant; it's O(n2) instead of O(n).

function hasDuplicates(array) {
    var valuesSoFar = [];
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (valuesSoFar.indexOf(value) !== -1) {
            return true;
        }
        valuesSoFar.push(value);
    }
    return false;
}

The difference is simply that we use an array instead of a hash table for valuesSoFar, since JavaScript "hash tables" (i.e. objects) only have string keys. This means we lose the O(1) lookup time of in, instead getting an O(n) lookup time of indexOf.

Cheery answered 11/9, 2011 at 6:15 Comment(9)
About the first example you gave. Isn't the validation exactly the other way around? If your function is named hasDuplicates, then it should check if the set's size actually shrunk during the process of casting it, right? Therefore the boolean operator should be !== and not ===Amieeamiel
pls edit. I can't edit as I'm not changing more than 6 characters.Amieeamiel
According to MDN IE11 does not the support the constructor used in the first exampleFulmination
Normal JS version returns true for the following array: [1, '1']Nari
Thus "if you only need string values in the array" preceding the answer.Cheery
However it is not supported in IE11 as you mentioned.It would return size 0 .Try this in IE 11 new Set([1, 2, 3, 4, 5])Alliterate
Solution 1 will not work for JSON objects check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Disallow
Hi! Just wanted to contribute with another solution that I ended up using. Just in case is useful for somebody: [1,2,3,4,4].filter( (v, i, arr) => i !== arr.indexOf(v) ). It will return an array with the repeated values (except the 1st occurrence).Felicio
ES2015 is a god.Bailee
M
21

One line solutions with ES6

const arr1 = ['hello','goodbye','hey'] 
const arr2 = ['hello','goodbye','hello'] 

const hasDuplicates = (arr) => arr.length !== new Set(arr).size;
console.log(hasDuplicates(arr1)) //return false because no duplicates exist
console.log(hasDuplicates(arr2)) //return true because duplicates exist

const s1 = ['hello','goodbye','hey'].some((e, i, arr) => arr.indexOf(e) !== i)
const s2 = ['hello','goodbye','hello'].some((e, i, arr) => arr.indexOf(e) !== i);

console.log(s1) //return false because no duplicates exist
console.log(s2) //return true because duplicates exist
Moxa answered 20/1, 2022 at 7:56 Comment(1)
FYI - Set has a .size property, so you don't have to spread into an array to get .lengthTermagant
G
19

You could use SET to remove duplicates and compare, If you copy the array into a set it will remove any duplicates. Then simply compare the length of the array to the size of the set.

function hasDuplicates(a) {

  const noDups = new Set(a);

  return a.length !== noDups.size;
}
Gennagennaro answered 23/3, 2021 at 19:33 Comment(0)
E
6

Another approach (also for object/array elements within the array1) could be2:

function chkDuplicates(arr,justCheck){
  var len = arr.length, tmp = {}, arrtmp = arr.slice(), dupes = [];
  arrtmp.sort();
  while(len--){
   var val = arrtmp[len];
   if (/nul|nan|infini/i.test(String(val))){
     val = String(val);
    }
    if (tmp[JSON.stringify(val)]){
       if (justCheck) {return true;}
       dupes.push(val);
    }
    tmp[JSON.stringify(val)] = true;
  }
  return justCheck ? false : dupes.length ? dupes : null;
}
//usages
chkDuplicates([1,2,3,4,5],true);                           //=> false
chkDuplicates([1,2,3,4,5,9,10,5,1,2],true);                //=> true
chkDuplicates([{a:1,b:2},1,2,3,4,{a:1,b:2},[1,2,3]],true); //=> true
chkDuplicates([null,1,2,3,4,{a:1,b:2},NaN],true);          //=> false
chkDuplicates([1,2,3,4,5,1,2]);                            //=> [1,2]
chkDuplicates([1,2,3,4,5]);                                //=> null

See also...

1 needs a browser that supports JSON, or a JSON library if not.
2 edit: function can now be used for simple check or to return an array of duplicate values

Eastbourne answered 11/9, 2011 at 6:23 Comment(4)
Non-showstopper issues worth being aware of: 1) mutates the original array to be sorted; 2) does not differentiate between null, NaN, Infinity, +Infinity, and -Infinity; 3) objects are considered equal if they have the same own-properties, even if they have different prototypes.Cheery
@Domenic: yep, should've mentioned it. Edited to circumvent mutation of original array.Eastbourne
@Domenic: corrected for null/NaN/[+/-]Infinity, see edits.Eastbourne
@Domenic: Issue 3) is actually not a problem for me, because it is exactly what I want. I don't care about the prototype, just the values.Confront
B
4

You can take benefit of indexOf and lastIndexOf. if both indexes are not same, you have duplicate.

function containsDuplicates(a) {
  for (let i = 0; i < a.length; i++) {
    if (a.indexOf(a[i]) !== a.lastIndexOf(a[i])) {
      return true
    }
  }
  return false
}
Beaverette answered 27/5, 2021 at 8:57 Comment(0)
B
4

If you are dealing with simple values, you can use array.some() and indexOf()

for example let's say vals is ["b", "a", "a", "c"]

const allUnique = !vals.some((v, i) => vals.indexOf(v) < i);

some() will return true if any expression returns true. Here we'll iterate values (from the index 0) and call the indexOf() that will return the index of the first occurrence of given item (or -1 if not in the array). If its id is smaller that the current one, there must be at least one same value before it. thus iteration 3 will return true as "a" (at index 2) is first found at index 1.

Benzofuran answered 26/8, 2021 at 6:36 Comment(0)
C
3

is just simple, you can use the Array.prototype.every function

function isUnique(arr) {
  const isAllUniqueItems = input.every((value, index, arr) => {
    return arr.indexOf(value) === index; //check if any duplicate value is in other index
  });

  return isAllUniqueItems;
}
Candelabra answered 20/1, 2022 at 7:14 Comment(1)
This is a good solution if not using Set, since it uses the minimal amount of comparisons needed for a linear search approach + it also allows doing other checks at the same time, for example that no value in the array is null, with early exit as soon as a non-satisfying element is found (instead of first checking for duplicates and then check for other conditions after).Hedva
C
2

Why use this method:

I think this is the best way to do it when dealing with multiple arrays and loops, this exaple is very simple but in some cases such when iterating with several loops and iterating through objects this is the most reliable and optimal way to do it.

Explanation:

In this example the array is iterated, element is the same as array[i] i being the position of the array that the loop is currently on, then the function checks the position in the read array which is initialized as empty, if the element is not in the read array it'll return -1 and it'll be pushed to the read array, else it'll return its position and won't be pushed, once all the element of array has been iterated the read array will be printed to console

let array = [1, 2, 3, 4, 5, 1, 2, 3, 5]
let read = []

array.forEach(element => {
  if (read.indexOf(element) == -1) {
    read.push(element)
    console.log("This is the first time" + element + " appears in the array")
  } else {
    console.log(element + " is already in the array")
  }
})

console.log(read)
Cabalism answered 19/7, 2023 at 8:14 Comment(0)
T
1

One nice thing about solutions that use Set is O(1) performance on looking up existing items in a list, rather than having to loop back over it.

One nice thing about solutions that use Some is short-circuiting when the duplicate is found early, so you don't have to continue evaluating the rest of the array when the condition is already met.

One solution that combines both is to incrementally build a set, early terminate if the current element exists in the set, otherwise add it and move on to the next element.

const hasDuplicates = (arr) => {
  let set = new Set()
  return arr.some(el => {
    if (set.has(el)) return true
    set.add(el)
  })
}

hasDuplicates(["a","b","b"]) // true
hasDuplicates(["a","b","c"]) // false

According to JSBench.me, should preform pretty well for the varried use cases. The set size approach is fastest with no dupes, and checking some + indexOf is fatest with a very early dupe, but this solution performs well in both scenarios, making it a good all-around implementation.

Termagant answered 22/1, 2022 at 14:2 Comment(0)
M
0
    this.selectedExam = [];
    // example exam obj: {examId:1, name:'ExamName'}
    onExamSelect(exam: any) {
    if(!this.selectedExam.includes(exam?.name)){ 
      this.selectedExam.push(exam?.name);
    }}

In the above code, I have taken an array and on triggering a particular function (onExamSelect) we are checking duplicates and pushing unique elements.

Mestizo answered 23/3, 2023 at 23:48 Comment(0)
A
0

In 2023, this is the best way to do it for an array of objects, checking strict equality. A Set cannot do this.

const answerTableRows = [
    { rk: "u", pk: "1", },
    { rk: "u", pk: "2", },
    { rk: "u", pk: "1", }, // get rid of this one
    { rk: "x", pk: "y" },
];
let uniqueAnswerTableRows = answerTableRows.filter((v, i, a) => {
    return a.findIndex(t => (t.rk === v.rk && t.pk === v.pk)) === i;
});
console.log(uniqueAnswerTableRows);
// [ { rk: 'u', pk: '1' }, { rk: 'u', pk: '2' }, { rk: 'x', pk: 'y' } ]
Alamo answered 2/11, 2023 at 18:55 Comment(0)
P
0

Demo

function hasDuplicates(array) {
        var set = new Set();
        for (var i = 0; i < array.length; i++) {
            if (set.has(array[i])) {
                return true;
            } else {
                set.add(array[i]);
            }
        }
        return false;
    }
    
    var arr = [1, 2, 3, 4, 5, 6];
    console.log(hasDuplicates(arr)); // Output: false
    
    var arrWithDuplicates = [1, 2, 3, 4, 4, 5];
    console.log(hasDuplicates(arrWithDuplicates)); // Output: true
Philip answered 20/2, 2024 at 6:38 Comment(0)
M
-4
function hasAllUniqueChars( s ){ 
    for(let c=0; c<s.length; c++){
        for(let d=c+1; d<s.length; d++){
            if((s[c]==s[d])){
                return false;
            }
        }
    }
    return true;
}
Marcos answered 27/4, 2021 at 12:54 Comment(1)
Welcome to Stackoverflow. It would be great if you'd explain your answer instead of only posting some code. Thanks!Quadrate

© 2022 - 2025 — McMap. All rights reserved.