美文网首页
Symmetric Difference

Symmetric Difference

作者: Oracle_c113 | 来源:发表于2017-10-12 20:59 被阅读0次

要求

创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference)(△or⊕)数组.

例如:

sym([1, 2, 3], [5, 2, 1, 4])应该返回[3, 4, 5].

sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该返回[1, 4, 5].

分析

1.function sym(args)只获取参数中的第一个数组。。。

2.[1, 1, 2, 5]当做[1, 2, 5]算,因此需要一个去重函数;

3.检查后一个数组里的元素若出现在前一个数组,则移除前一个数组的元素,否则,添加至前一个数组。

解决

改进

```function sym(args){

var a=Array.from(arguments);

a=a.reduce(function(prev, curv, index, array){

var a = prev.filter(function(item){

return curv.indexOf(item) < 0;

});

var b = curv.filter(function(item){

return prev.indexOf(item) < 0;

});

return a.concat(b);

});

return a.filter(function(item,index,array){

return array.indexOf(item) == index;

});

}```

测试

sym([1, 2, 3], [5, 2, 1, 4])应该返回[3, 4, 5].

sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该返回[1, 4, 5].

sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该只包含三个元素.

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])应该只包含八个元素.

相关文章

  • FCC高级编程篇之Symmetric Difference

    Symmetric Difference Create a function that takes two or ...

  • Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or...

  • Symmetric Difference

    function sym(args) { var arr = []; var answer = []; ar...

  • Symmetric Difference

    要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference)(△or...

  • Symmetric Difference

    要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference)(△or...

  • Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or...

  • FCC对等差分

    题目 创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△...

  • FreeCodeCamp筆記之:Symmetric Differ

    题目 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference)(△or...

  • 101 Symmetric Tree

    title: Symmetric Treetags:- symmetric-tree- No.101- simpl...

  • DES

    You should Know First 1.Symmetric cipher Symmetric cipher...

网友评论

      本文标题:Symmetric Difference

      本文链接:https://www.haomeiwen.com/subject/fxnoyxtx.html