Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

后端 未结 30 3238
再見小時候
再見小時候 2020-11-21 04:35

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\'

相关标签:
30条回答
  • 2020-11-21 05:11

    You can add this function, or tweak it and add it to Javascript's Array prototype:

    Array.prototype.unique = function () {
        var r = new Array();
        o:for(var i = 0, n = this.length; i < n; i++)
        {
            for(var x = 0, y = r.length; x < y; x++)
            {
                if(r[x]==this[i])
                {
                    alert('this is a DUPE!');
                    continue o;
                }
            }
            r[r.length] = this[i];
        }
        return r;
    }
    
    var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
    var unique = arr.unique();
    alert(unique);
    
    0 讨论(0)
  • 2020-11-21 05:12

    This should get you what you want, Just the duplicates.

    function find_duplicates(arr) {
      var len=arr.length,
          out=[],
          counts={};
    
      for (var i=0;i<len;i++) {
        var item = arr[i];
        counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
        if (counts[item] === 2) {
          out.push(item);
        }
      }
    
      return out;
    }
    
    find_duplicates(['one',2,3,4,4,4,5,6,7,7,7,'pig','one']); // -> ['one',4,7] in no particular order.
    
    0 讨论(0)
  • 2020-11-21 05:13

    Shortest vanilla JS:

    [1,1,2,2,2,3].filter((v,i,a) => a.indexOf(v) !== i) // [1, 2, 2]
    
    0 讨论(0)
  • 2020-11-21 05:14

    You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than O(n2):

    const findDuplicates = (arr) => {
      let sorted_arr = arr.slice().sort(); // You can define the comparing function here. 
      // JS by default uses a crappy string compare.
      // (we use slice to clone the array so the
      // original array won't be modified)
      let results = [];
      for (let i = 0; i < sorted_arr.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
          results.push(sorted_arr[i]);
        }
      }
      return results;
    }
    
    let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7];
    console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);

    In case, if you are to return as a function for duplicates. This is for similar type of case.

    Reference: https://stackoverflow.com/a/57532964/8119511

    0 讨论(0)
  • 2020-11-21 05:15

    one liner simple way

    var arr = [9,1,2,4,3,4,9]
    console.log(arr.filter((ele,indx)=>indx!==arr.indexOf(ele))) //get the duplicates
    console.log(arr.filter((ele,indx)=>indx===arr.indexOf(ele))) //remove the duplicates

    0 讨论(0)
  • 2020-11-21 05:17

    Find non-unique values from 3 arrays (or more):

    ES2015

    //                                                                              
    0 讨论(0)
提交回复
热议问题