FCC高级编程篇之Symmetric Difference
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的更多相关文章
- FCC高级编程篇之Validate US Telephone Numbers
Validate US Telephone Numbers Return true if the passed string is a valid US phone number. The user ...
- FCC高级编程篇之Make a Person
Make a Person Fill in the object constructor with the following methods below: getfirstname() getLas ...
- FCC高级编程篇之Exact Change
Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...
- FCC高级编程篇之Record Collection
Record Collection You are given a JSON object representing a part of your musical album collection. ...
- FCC(ES6写法) Symmetric Difference
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...
- (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- unix环境高级编程基础知识之第二篇(3)
看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
- C++面向对象高级编程(三)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...
随机推荐
- Vue.js 2.x Development Build With Hot Reloading For External Server (using Webpack template)
This article assuming you created your project using webpack template. vue init webpack <PROJECT_ ...
- php 生成不重复的随机字符串
md5(uniqid(md5(microtime(true)),true))
- Dynamic dispatch mechanisms
Normally, in a typed language, the dispatch mechanism will be performed based on the type of the arg ...
- win10x64位系统中nodejs的安装和配置
官网http://nodejs.cn/download/ 2.下载完成后点击安装包 下一步,安装过的,这里根据自己的需求选择.选择第直接正常安装. 这一步是安装的内容,第一个是安装所有的模块,建议全部 ...
- Django -查询数据库相关操作
一. 内置Admin 依赖APP: django.contrib.auth django.contrib.contenttypes django.contrib.messages django.con ...
- 15 个经常使用的 SQL Server 高级语法
1.case-end (详细的值) case后面有值,相当于c#中的switch case 注意:case后必须有条件,而且when后面必须是值不能为条件. -----------------case ...
- H2数据库入门使用
H2数据库入门使用 学习了: https://www.cnblogs.com/xdp-gacl/p/4171024.html http://www.cnblogs.com/xdp-gacl/p/417 ...
- Linux文件查找命令具体解释-which whereis find locate
原创BLog.转载请注明出处 http://blog.csdn.net/hello_hwc? viewmode=contents which命令 首先查看man which的说明 which - sh ...
- Qt 3D教程(三)实现对模型材质參数的控制
Qt 3D教程(三)实现对模型材质參数的控制 蒋彩阳原创文章,首发地址:http://blog.csdn.net/gamesdev/article/details/47131841.欢迎同行前来探讨. ...
- D3D 线列 小样例
画两条线 #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib" ...