Symmetric Difference

Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays.

Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term "symmetric difference" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

Here are some helpful links:

对称差分是指某一元素仅存在于一个集合而不存在于另一个集合。

可类比于数组去重,两者有相似之处。数组去重判断数组中元素的下标与找到的第一个该元素的下标是否相等,而对称差分判断某元素在另一个集合中是否存在。

首先,函数参数可以有多个,因而得把参数合并为一个数组。

function sym(args) {
let arr = Array.prototype.slice.call(arguments); return arr;
}

arguments是一个类似于数组的对象,但并非是数组,用Array.prototype.slice.call()方法将其转换为数组。

前面说了,对称差分和数组去重有类似之处,这里可以先进行合并,然后对合并后的数组进行过滤。

function sym(args) {
let 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;
})
});
}

但这还有个问题,如果一个数组中有重复的元素,那过滤后的数组中依然会重复,按照题意,去掉多余重复的元素。

function sym(args) {
let 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;
});
});
}

测试结果如下图所示。

FCC高级编程篇之Symmetric Difference的更多相关文章

  1. FCC高级编程篇之Validate US Telephone Numbers

    Validate US Telephone Numbers Return true if the passed string is a valid US phone number. The user ...

  2. FCC高级编程篇之Make a Person

    Make a Person Fill in the object constructor with the following methods below: getfirstname() getLas ...

  3. FCC高级编程篇之Exact Change

    Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...

  4. FCC高级编程篇之Record Collection

    Record Collection You are given a JSON object representing a part of your musical album collection. ...

  5. FCC(ES6写法) Symmetric Difference

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

  6. (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  7. unix环境高级编程基础知识之第二篇(3)

    看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...

  8. C++面向对象高级编程(四)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...

  9. C++面向对象高级编程(三)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...

随机推荐

  1. C语言中文件定位函数总结

    C语言中文件定位函数主要是:fseek, ftell, fsetpos, fgetpos. 先来讲前两个函数,这是最基本的定位函数: fseek函数:能把文件指针移动到文件任何位置,其原型是:int ...

  2. SQL基本语句:1.模式 3.索引

    每次很长时间不用sql语句之后,都需要把基础的捡一捡,索性做个笔记,以后可以长看

  3. 路飞学城Python-Day24(practise)

    本章总结 练习题 什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件)

  4. CentOS 7.5安装pycharm

    环境 安装环境: windows 10 Pro CentOS Linux release 7.6.1810 (Core) VMWare Workstation 15 Pro 安装图形化界面包 首先更新 ...

  5. ZOJ 1081 Points Within( 判断点在多边形内外 )

    链接:传送门 题意:给出n个点围成的一个多边形,现在有m个点p,询问p是否在多边形内,你可以认为这些点均不同且输入的顶点是多边形中相邻的两个顶点,最后的顶点与第一个相邻并且每一个顶点都连接两条边( 左 ...

  6. 一、frp官方中文文档

    frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp, http, https 协议. 目录 frp 的作用 开发状态 架构 使用示例 通过 ssh 访问公司内网机器 通过自定义 ...

  7. keil5下载程序时出现“internal command error”解决方法

    今天下载的时候,插入下载器,查看Debug可用看到“internal command error”,一直找不到原因,后来查看上一个工程才发现,上一个程序禁用了Seral Wire 引脚,导致的. 解决 ...

  8. 【codeforces 757E】Bash Plays with Functions

    [题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...

  9. MyBatis学习总结(7)——Mybatis缓存

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  10. WinServer-PowerShell基础

    命令的参数: [-name] 这个参数必须要有,string表示name参数接受什么样的实参,<>表示参数可以接受的实参类型,通常出现set get add都会伴随着必须参数 [-name ...