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

后端 未结 30 3151
再見小時候
再見小時候 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:22

    Here's the simplest solution I could think of:

        const arr = [-1, 2, 2, 2, 0, 0, 0, 500, -1, 'a', 'a', 'a']
    
        const filtered = arr.filter((el, index) => arr.indexOf(el) !== index)
        // => filtered = [ 2, 2, 0, 0, -1, 'a', 'a' ]
    
        const duplicates = [...new Set(filtered)]
    
        console.log(duplicates)
        // => [ 2, 0, -1, 'a' ]
    

    That's it.

    Note:

    1. It works with any numbers including 0, strings and negative numbers e.g. -1 - Related question: Get all unique values in a JavaScript array (remove duplicates)

    2. The original array arr is preserved (filter returns the new array instead of modifying the original)

    3. The filtered array contains all duplicates; it can also contain more than 1 same value (e.g. our filtered array here is [ 2, 2, 0, 0, -1, 'a', 'a' ])

    4. If you want to get only values that are duplicated (you don't want to have multiple duplicates with the same value) you can use [...new Set(filtered)] (ES6 has an object Set which can store only unique values)

    Hope this helps.

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

    Following logic will be easier and faster

    // @Param:data:Array that is the source 
    // @Return : Array that have the duplicate entries
    findDuplicates(data: Array<any>): Array<any> {
            return Array.from(new Set(data)).filter((value) => data.indexOf(value) !== data.lastIndexOf(value));
          }
    

    Advantages :

    1. Single line :-P
    2. All inbuilt data structure helping in improving the efficiency
    3. Faster

    Description of Logic :

    1. Converting to set to remove all duplicates
    2. Iterating through the set values
    3. With each set value check in the source array for the condition "values first index is not equal to the last index" == > Then inferred as duplicate else it is 'unique'

    Note: map() and filter() methods are efficient and faster.

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

    using underscore.js

    function hasDuplicate(arr){
        return (arr.length != _.uniq(arr).length);
    }
    
    0 讨论(0)
  • 2020-11-21 05:28
    var a = [324,3,32,5,52,2100,1,20,2,3,3,2,2,2,1,1,1].sort();
    a.filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});
    

    or when added to the prototyp.chain of Array

    //copy and paste: without error handling
    Array.prototype.unique = 
       function(){return this.sort().filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});}
    

    See here: https://gist.github.com/1305056

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

    This answer might also be helpful, it leverages js reduce operator/method to remove duplicates from array.

    const result = [1, 2, 2, 3, 3, 3, 3].reduce((x, y) => x.includes(y) ? x : [...x, y], []);
    
    console.log(result);

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

    Simple code with ES6 syntax (return sorted array of duplicates):

    let duplicates = a => {d=[]; a.sort((a,b) => a-b).reduce((a,b)=>{a==b&&!d.includes(a)&&d.push(a); return b}); return d};
    

    How to use:

    duplicates([1,2,3,10,10,2,3,3,10]);
    
    0 讨论(0)
提交回复
热议问题