function sym(args) {
var arr = Array.prototype.slice.call(arguments); return arr.reduce((arr1, arr2) => {
return arr1.concat(arr2).filter((val) => {
return arr1.indexOf(val) === -1 || arr2.indexOf(val) === -1;
}).filter((val, index, arr) => {
return arr.indexOf(val) === index;
});
});
}

题目:

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

分析:

  Array.reduce()  Symmetric Difference

  Array.prototype.slice()

    概述:方法返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

  arguments

    概述:arguments 是一个对应于传递给函数的参数的类数组对象。    

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments

  Array.prototype.filter()

    概述:方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

  "=>"采用的是新的标准 ES6标准。这个标准会渐渐流行,有警告无伤大雅的。

  因为这是我目前见到思路最清晰,代码最简洁的写法了,所以摘录下来慢慢消化。

本文参考博客:

  澄酱

    https://www.cnblogs.com/laoho/p/7787658.html

  

Symmetric Difference FreeCodeCamp的更多相关文章

  1. js-FCC算法-Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...

  2. FCC(ES6写法) Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...

  3. [Advanced Algorithm] - Symmetric Difference

    题目 创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3}和集合 B = ...

  4. FCC高级编程篇之Symmetric Difference

    Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...

  5. hackerrank---Sets - Symmetric Difference

    题目链接 集合操作 附上代码: M = int(input()) m = set(map(int, raw_input().strip().split())) N = int(input()) n = ...

  6. Symmetric Difference

    function sym(args) { //return args; var arr = []; for(var i = 0; i < arguments.length; i++){ arr. ...

  7. FreeCodeCamp 高级算法(个人向)

    freecodecamp 高级算法地址戳这里. freecodecamp的初级和中级算法,基本给个思路就能完成,而高级算法稍微麻烦了一点,所以我会把自己的解答思路写清楚,如果有错误或者更好的解法,欢迎 ...

  8. Symmetric Difference-freecodecamp算法题目

    Symmetric Difference 1.要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference) 例子:给出两个集合 (如集合 A = {1, ...

  9. fcc的高级算法题

    核心提示:本部分一个9道题,给定时间50小时.属于fcc前端学习的"高级编程脚本"题,对于初学者来说,确实算是"高级"了.如果只想着闭门造车,50小时确实也不过 ...

随机推荐

  1. 我理解的数据结构(三)—— 队列(Queue)

    我理解的数据结构(三)-- 队列(Queue) 一.队列 队列是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另一端(队首)取出元素 队列是一种先进先出的数据结 ...

  2. 1.3 eclipse中配置Tomcat

    下载并成功安装Eclipse和Tomcat():   打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 步骤阅读 3 找到Server下方的Runtime Envi ...

  3. K - Transformation

    K - Transformation HDU - 4578 思路:发现自己的错误竟然是zz般的少了一个取模   ε=ε=ε=(#>д<)ノ #include<cstdio> # ...

  4. mysql 服务器监控系列-黄杉 mysqldba

    http://blog.csdn.net/mchdba/article/category/2220809

  5. HDU 4527

    搞了好久,发现自己是想法没错的,错在输入,必须是while(){} #include <iostream> #include <cstdio> #include <alg ...

  6. POJ 1021 人品题

    报告见代码.. #include <iostream> #include <cstdio> #include <cstring> #include <algo ...

  7. [CSS3] :empty Selector

    When the element has empty content, you might want to display some text to idicate the compoent is l ...

  8. 使用Html5和Js进行拖动

    function init() {             var source = document.getElementById("dragme");             ...

  9. 用Thinphp发送电子邮件的方法

    好长时间没有动php了,突然想用thinkphp发送电子邮件,可是查阅了书籍都写的非常乱.没有继续看下去.这里找到了一个比較好的方法: 第一步:首先我们要引入一个外部类库:Mail.class.php ...

  10. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...