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]];
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]];
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)
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; }
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>
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);
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);