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

后端 未结 5 1477
生来不讨喜
生来不讨喜 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: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)

提交回复
热议问题