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

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

    Find duplicate values in an array

    This should be one of the shortest ways to actually find duplicate values in an array. As specifically asked for by the OP, this does not remove duplicates but finds them.

    var input = [1, 2, 3, 1, 3, 1];
    
    var duplicates = input.reduce(function(acc, el, i, arr) {
      if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
    }, []);
    
    document.write(duplicates); // = 1,3 (actual array == [1, 3])

    This doesn't need sorting or any third party framework. It also doesn't need manual loops. It works with every value indexOf() (or to be clearer: the strict comparision operator) supports.

    Because of reduce() and indexOf() it needs at least IE 9.

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

    Here is mine simple and one line solution.

    It searches not unique elements first, then makes found array unique with the use of Set.

    So we have array of duplicates in the end.

    var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];
    
    console.log([...new Set(
      array.filter((value, index, self) => self.indexOf(value) !== index))]
    );

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

    var arr = [2, 1, 2, 2, 4, 4, 2, 5];
    
    function returnDuplicates(arr) {
      return arr.reduce(function(dupes, val, i) {
        if (arr.indexOf(val) !== i && dupes.indexOf(val) === -1) {
          dupes.push(val);
        }
        return dupes;
      }, []);
    }
    
    alert(returnDuplicates(arr));

    This function avoids the sorting step and uses the reduce() method to push duplicates to a new array if it doesn't already exist in it.

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

    I have just figured out a simple way to achieve this using an Array filter

        var list = [9, 9, 111, 2, 3, 4, 4, 5, 7];
        
        // Filter 1: to find all duplicates elements
        var duplicates = list.filter(function(value,index,self) {
           return self.indexOf(value) !== self.lastIndexOf(value) && self.indexOf(value) === index;
        });
        
        console.log(duplicates);

    0 讨论(0)
  • 2020-11-21 05:20
    var a = ["a","a","b","c","c"];
    
    a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
    
    0 讨论(0)
  • 2020-11-21 05:21

    This is my answer from the duplicate thread (!):

    When writing this entry 2014 - all examples were for-loops or jQuery. Javascript has the perfect tools for this: sort, map and reduce.

    Find duplicate items

    var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
    
    var uniq = names
      .map((name) => {
        return {
          count: 1,
          name: name
        }
      })
      .reduce((a, b) => {
        a[b.name] = (a[b.name] || 0) + b.count
        return a
      }, {})
    
    var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
    
    console.log(duplicates) // [ 'Nancy' ]

    More functional syntax:

    @Dmytro-Laptin pointed out some code code be removed. This is a more compact version of the same code. Using some ES6 tricks and higher order functions:

    const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
    
    const count = names =>
      names.reduce((a, b) => ({ ...a,
        [b]: (a[b] || 0) + 1
      }), {}) // don't forget to initialize the accumulator
    
    const duplicates = dict =>
      Object.keys(dict).filter((a) => dict[a] > 1)
    
    console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
    console.log(duplicates(count(names))) // [ 'Nancy' ]

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