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

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

    Here is a very light and easy way:

    var codes = dc_1.split(',');
    var i = codes.length;
    while (i--) {
      if (codes.indexOf(codes[i]) != i) {
        codes.splice(i,1);
      }
    }
    
    0 讨论(0)
  • 2020-11-21 05:34

    The following function (a variation of the eliminateDuplicates function already mentioned) seems to do the trick, returning test2,1,7,5 for the input ["test", "test2", "test2", 1, 1, 1, 2, 3, 4, 5, 6, 7, 7, 10, 22, 43, 1, 5, 8]

    Note that the problem is stranger in JavaScript than in most other languages, because a JavaScript array can hold just about anything. Note that solutions that use sorting might need to provide an appropriate sorting function--I haven't tried that route yet.

    This particular implementation works for (at least) strings and numbers.

    function findDuplicates(arr) {
        var i,
            len=arr.length,
            out=[],
            obj={};
    
        for (i=0;i<len;i++) {
            if (obj[arr[i]] != null) {
                if (!obj[arr[i]]) {
                    out.push(arr[i]);
                    obj[arr[i]] = 1;
                }
            } else {
                obj[arr[i]] = 0;            
            }
        }
        return out;
    }
    
    0 讨论(0)
  • 2020-11-21 05:34

    Using "includes" to test if the element already exists.

    var arr = [1, 1, 4, 5, 5], darr = [], duplicates = [];
    
    for(var i = 0; i < arr.length; i++){
      if(darr.includes(arr[i]) && !duplicates.includes(arr[i]))
        duplicates.push(arr[i])
      else
        darr.push(arr[i]);
    }
    
    console.log(duplicates);
    <h3>Array with duplicates</h3>
    <p>[1, 1, 4, 5, 5]</p>
    <h3>Array with distinct elements</h3>
    <p>[1, 4, 5]</p>
    <h3>duplicate values are</h3>
    <p>[1, 5]</p>

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

    If you want to elimate the duplicates, try this great solution:

    function eliminateDuplicates(arr) {
      var i,
          len = arr.length,
          out = [],
          obj = {};
    
      for (i = 0; i < len; i++) {
        obj[arr[i]] = 0;
      }
      for (i in obj) {
        out.push(i);
      }
      return out;
    }
    

    Source: http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/

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

    UPDATED: Short one-liner to get the duplicates:

    [1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i) // [2, 4]
    

    To get the array without duplicates simply invert the condition:

    [1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) === i) // [1, 2, 3, 4]
    

    I simply did not think about filter() in my old answer below ;)


    When all you need is to check that there are no duplicates as asked in this question you can use the every() method:

    [1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true
    
    [1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
    

    Note that every() doesn't work for IE 8 and below.

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

    With ES6 (or using Babel or Typescipt) you can simply do:

    var duplicates = myArray.filter(i => myArray.filter(ii => ii === i).length > 1);
    

    https://es6console.com/j58euhbt/

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