fastest way to detect if duplicate entry exists in javascript array?

前端 未结 8 2020
时光说笑
时光说笑 2021-02-06 09:54
var arr = [\'test0\',\'test2\',\'test0\'];

Like the above,there are two identical entries with value \"test0\",how to check it most efficiently?

相关标签:
8条回答
  • 2021-02-06 10:29

    Loop stops when found first duplicate:

    function has_duplicates(arr) {
    
        var x = {}, len = arr.length;
        for (var i = 0; i < len; i++) {
            if (x[arr[i]]) {
                 return true;
            }
            x[arr[i]] = true;
        }
        return false;
    
    }
    

    Edit (fix 'toString' issue):

    function has_duplicates(arr) {
    
        var x = {}, len = arr.length;
        for (var i = 0; i < len; i++) {
            if (x[arr[i]] === true) {
                 return true;
            }
            x[arr[i]] = true;
        }
        return false;
    
    }
    

    this will correct for case has_duplicates(['toString']); etc..

    0 讨论(0)
  • 2021-02-06 10:31

    If you sort the array, the duplicates are next to each other so that they are easy to find:

    arr.sort();
    var last = arr[0];
    for (var i=1; i<arr.length; i++) {
       if (arr[i] == last) alert('Duplicate : '+last);
       last = arr[i];
    }
    
    0 讨论(0)
  • 2021-02-06 10:34

    Assuming all you want is to detect how many duplicates of 'test0' are in the array. I guess an easy way to do that is to use the join method to transform the array in a string, and then use the match method.

    var arr= ['test0','test2','test0'];
    var str = arr.join();
    
    console.log(str) //"test0,test2,test0"
    
    var duplicates = str.match(/test0/g);
    var duplicateNumber = duplicates.length;
    
    console.log(duplicateNumber); //2
    
    0 讨论(0)
  • 2021-02-06 10:37

    You can convert the array to to a Set instance, then convert to an array and check if the length is same before and after the conversion.

    const hasDuplicates = (array) => {
      const arr = ['test0','test2','test0'];
      const set1 = new Set(array);
      const uniqueArray = [...set1];
      
      return array.length !== uniqueArray.length;
    };
    
    console.log(`Has duplicates : ${hasDuplicates(['test0','test2','test0'])}`);
    console.log(`Has duplicates : ${hasDuplicates(['test0','test2','test3'])}`);

    0 讨论(0)
  • 2021-02-06 10:38

    There are lots of answers here but not all of them "feel" nice... So I'll throw my hat in.

    If you are using lodash:

    function containsDuplicates(array) {
      return _.uniq(array).length !== array.length; 
    }
    

    If you can use ES6 Sets, it simply becomes:

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

    With vanilla javascript:

    function containsDuplicates(array) {
      return array
        .sort()
        .some(function (item, i, items) {
          return item === items[i + 1]
        })
    }
    

    However, sometimes you may want to check if the items are duplicated on a certain field.

    This is how I'd handle that:

    containsDuplicates([{country: 'AU'}, {country: 'UK'}, {country: 'AU'}], 'country')
    
    function containsDuplicates(array, attribute) {
      return array
        .map(function (item) { return item[attribute] })
        .sort()
        .some(function (item, i, items) {
          return item === items[i + 1]
        })
    }
    
    0 讨论(0)
  • 2021-02-06 10:45

    This will do the job on any array and is probably about as optimized as possible for handling the general case (finding a duplicate in any possible array). For more specific cases (e.g. arrays containing only strings) you could do better than this.

    function hasDuplicate(arr) {
        var i = arr.length, j, val;
    
        while (i--) {
            val = arr[i];
            j = i;
            while (j--) {
                if (arr[j] === val) {
                    return true;
                }
            }
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题