This particular piece of code is designed to match the entire array regardless of the value sequence and count. Take a look at the example below
// Insert your code here
Plunker: https://plnkr.co/edit/6jEByZHQhcwUopqqG4a2?p=preview
(Click on the link above and open the console to view the result)
The output will be ["2","3"] because "2" matches
"4" and "3" matches
"6".
// Insert your code here
var data = {
2: [ [1, 2], [3, 4, 5], [6, 7]],
3: [[1, 2],[3, 4],[6, 7]],
4: [[2, 1, 2, 1],[5, 3, 4],[6, 7]],
5: [[10, 2],[3, 4, 5],[60, 7]],
6: [[2, 1],[3, 4],[7, 6, 6]]
}
var dups = [];
//sorted and unique buffer.
var buff = _.clone(data);
_.forEach(buff, function(lvl1, key) {
buff[key] = _.map(lvl1, function(val) {
return _.uniq(_.sortBy(val));
});
});
_.forEach(buff, function(lvl1, key) {
//Matching each row with all others.
//e.g. 2 with 3, 2 with 4, 2 with 5 ..... 6 with 5.
_.forEach(buff, function(_lvl1, _key) {
//Skipping entries like 2 with 2, 3 with 3 and so on
if (key !== _key) {
console.log('compare ' + key + ' with ' + _key);
var x = _.filter(_lvl1, function(val, index) {
//Conditions below are only true because arrays are sorted and unique.
//Both must be of the same length
return val.length === lvl1[index].length
//Length of intersection of both values must be exactly equal
&& _.intersection(val, lvl1[index]).length === val.length;
});
//If count matches and key and _key are not in dups
// key and _key not in dups because we only want the following:
// 2 with 4 -> 2
// 4 with 2 -> ignore! (we don't want to include 4)
//I hope this makes sense
if (x.length === _lvl1.length && _.indexOf(dups, key) === -1 && _.indexOf(dups, _key) === -1) {
//Mark the key as duplicate
dups.push(key);
}
}
});
});
console.log(dups)
I trust this explanation is helpful!