[removed] how to remove duplicate arrays inside array of arrays

后端 未结 5 1451
生来不讨喜
生来不讨喜 2020-12-10 12:19

Input:

[[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]]

The output I want:

[[-1,-1,2],[-1,0,1]]
<         


        
相关标签:
5条回答
  • 2020-12-10 13:00

    jsfiddle

    Borrowing the array comparison code from this post

    // Warn if overriding existing method
    if(Array.prototype.equals)
        console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
    // attach the .equals method to Array's prototype to call it on any array
    Array.prototype.equals = function (array) {
        // if the other array is a falsy value, return
        if (!array)
            return false;
    
        // compare lengths - can save a lot of time 
        if (this.length != array.length)
            return false;
    
        for (var i = 0, l=this.length; i < l; i++) {
            // Check if we have nested arrays
            if (this[i] instanceof Array && array[i] instanceof Array) {
                // recurse into the nested arrays
                if (!this[i].equals(array[i]))
                    return false;       
            }           
            else if (this[i] != array[i]) { 
                // Warning - two different object instances will never be equal: {x:20} != {x:20}
                return false;   
            }           
        }       
        return true;
    }
    
    var old = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]], n = [];
    
    while(old.length) {
        var arr = old.shift(), matched = false;
    
      for(var i = 0, len = n.length; i < len; i++) {
        if (arr.equals(n[i])) {
            matched = true;
            break;
        }
      }
      if (!matched) {
        n.push(arr);
      }
    }
    
    0 讨论(0)
  • 2020-12-10 13:09

    There's already a good utility for that, try Lodash, one of the function of it is _.uniqWith, with that function you can do the following.

    <script src="/path/to/lodash.js"></script>
    <script>
        var aa = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]];
        console.log(aa);
        console.log(_.uniqWith(aa,_.isEqual));
    </script>
    
    0 讨论(0)
  • 2020-12-10 13:10

    You can create a hashMap and save values in it. This will always hold last value.

    var data = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]]
    
    var hashMap = {}
    
    data.forEach(function(arr){
      // If your subArrays can be in any order, you can use .sort to have consistant order
      hashMap[arr.join("|")] = arr;
    });
    
    var result = Object.keys(hashMap).map(function(k){
      return hashMap[k]
    })
    
    console.log(result)

    0 讨论(0)
  • 2020-12-10 13:11

    You won't really get around stringifying the arrays, as that's the simplest (and reasonably fast) way to compare them by value. So I'd go for

    Array.from(new Set(input.map(JSON.stringify)), JSON.parse)
    

    See also Remove Duplicates from JavaScript Array for other approaches, though most of them will require two values to be comparable by ===.

    0 讨论(0)
  • 2020-12-10 13:24

    Magic

    d.filter(( t={}, a=> !(t[a]=a in t) ));
    

    I assume your input data are in array d. Explanation here.

    let d = [[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,-1,2],[-1,0,1],[-1,0,1]];
    
    var r = d.filter((t={},a=>!(t[a]=a in t)));
    
    console.log(JSON.stringify(r));

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