Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Created April 24, 2016 11:47
Show Gist options
  • Select an option

  • Save black-black-cat/2ae07f46a64bbb2d6b393e692dbd8f55 to your computer and use it in GitHub Desktop.

Select an option

Save black-black-cat/2ae07f46a64bbb2d6b393e692dbd8f55 to your computer and use it in GitHub Desktop.
n个数组的对称差
// 先求两个的对称差,用求得的结果与其后的数组重复之前步骤
function sym() {
var ans = [];
var args = Array.prototype.slice.call(arguments);
var len = args.length;
var two;
function uniq(arr) {
var output = [];
var objMark = {};
arr.forEach(function(item, index) {
if (objMark[item] == null) {
output.push(item);
objMark[item] = 1;
}
})
return output;
}
if (len < 2) return false;
if (len > 2) {
two = args.splice(0, 2);
} else {
two = args;
}
if (len === 2) {
for (var i = 0; i < 2; i++) {
var tempArr = two[i].filter(function(el) {
var j;
var allHas = true;
for (j = 0; j < 2; j++) {
if (Array.isArray(two[j]) && two[j] !== two[i]) {
if (two[j].indexOf(el) !== -1) {
return false;
}
}
}
return true;
});
ans = ans.concat(tempArr);
}
return uniq(ans);
}
return sym.apply(this, [sym(two[0], two[1])].concat(args) );
}
var a = sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment