-2

I have an array:

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];

All I want is to filter them from duplicate arrays. So it will look like:

var arr = [[1,7],[2,6],[3,5],[4,4]];
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
kindguy123
  • 27
  • 3
  • Possible duplicate of [How to compare arrays in JavaScript?](http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Björn Jan 06 '17 at 12:56
  • Possible duplicate of [Remove duplicates from an array of objects in javascript](http://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Alec Jan 06 '17 at 12:56
  • Guys, these links you posted doesnt have any solution for my problem. – kindguy123 Jan 06 '17 at 12:57
  • Plain javascript? No jQuery or underscore? – Kiran Shinde Jan 06 '17 at 13:00
  • 1
    @kindguy123, does the order of items matter? just want to know, should I adjust my answer. It gives `[ [ "4", "4" ], [ "3", "5" ], [ "2", "6" ], [ "1", "7" ] ]` – RomanPerekhrest Jan 06 '17 at 13:11

5 Answers5

4

You can do this with filter(), some() and every().

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];

var result = arr.filter(function(e) {
  var r = this.some(function(a) {
    return e.every(function(b) {
      return a.includes(b)
    })
  })
  if (!r) {
    this.push(e);
    return e
  }
}, [])

console.log(result)

If you want to use arrow functions you can also write it like this.

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];

var result = arr.filter(function(e) {
  return !this.some(a =>e.every(b =>a.includes(b))) ? this.push(e) && e : false
}, [])

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
3

You could use a hash table and use the joined string of the sorted array as hash.

var arr = [[1, 7], [2, 6], [3, 5], [4, 4], [5, 3], [6, 2], [7, 1]],
    result = arr.filter(function(a) {
        var hash = a.slice().sort(function (a, b) { return a - b; }).join();
        if (!this[hash]) {
            this[hash] = true;
            return true;
        }
    }, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6 with Set

var asc = (a, b) => a - b,
    join = a => a.slice().sort(asc).join(),
    arr = [[1, 7], [2, 6], [3, 5], [4, 4], [5, 3], [6, 2], [7, 1]],
    result = arr.filter((hash => a => (h => !hash.has(h) && hash.add(h))(join(a)))(new Set));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Here is solution:

First of all, you should sort arrays to look like : [[1,7],[2,6],[3,5],[4,4],[3,5],[2,6],[1,7]], and then eliminate duplicates.

Use filter function for filtering the values from array. seen its a dictionary and i'm using it for verify if the current value was found in it(seen dictionary).If yes, "remove" value from array. If not,set that the current array was used and go to the next item. I said "remove" because filter function creates a new array.

filter function is easy to understand. Learn more about it, here

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];
arr.forEach(function(array){
    array.sort((a,b)=>a>b);
});
var seen = {};
var uniqueArray = arr.filter(function(item) {
      return seen.hasOwnProperty(item) ? false : (seen[item] = true);
 });
console.log(uniqueArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

The solution using
Array.prototype.map() , Array.prototype.filter(),
Array.prototype.join(), String.prototype.split() functions:

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];

result = arr.map(function (a) {
    a.sort();
    return a.join();
}).filter(function (inner_arr, idx, a) {
    return idx === a.lastIndexOf(inner_arr);
}).map(function(s){
    return s.split(',');
});

console.log(result);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

As i can see, all solutions keep original array intact. If that's not problem, here is one more meat&potatoes™, straightforward solution:

var arr = [[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]];


temp=[];
unique=[];
for(i=0;i<arr.length;i++) {
str=arr[i].sort().toString();
if(temp.indexOf(str)===-1) {
temp.push(str);
unique.push(arr[i]);
}
}



console.log(unique);
sinisake
  • 11,240
  • 2
  • 19
  • 27