题目

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

给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2, 3, 4}), 而数学术语 "对等差分" 的集合就是指由所有只在两个集合其中之一的元素组成的集合(A △ B = C = {1, 4}). 对于传入的额外集合 (如 D = {2, 3}), 你应该安装前面原则求前两个集合的结果与新集合的对等差分集合 (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).

提示

Array.reduce()

Symmetric Difference

测试用例

  • sym([1, 2, 3], [5, 2, 1, 4]) 应该返回 [3, 4, 5].
  • sym([1, 2, 3], [5, 2, 1, 4]) 应该只包含三个元素.
  • sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 应该返回 [1, 4, 5]
  • sym([1, 2, 5], [2, 3, 5], [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]) 应该返回 [2, 3, 4, 6, 7].
  • sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该只包含五个元素.
  • sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该返回 [1, 2, 4, 5, 6, 7, 8, 9].
  • sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该只包含八个元素.

分析思路

  1. 由于传入参数是可变的,所以会用到 arguments,又由于arguments只有 length 属性,所以要把 arguments 转换成普通数组,代码如下:

    var argArray = Array.from(arguments);
  2. 根据题目,分析 A,B 中互相存在的元素,都需要过滤掉,用下面的代码过滤:
  function diff(A, B) {
return A.filter(function(val) {
return !B.includes(val);
});
}

这个函数是去掉 A 中所有 B 拥有的元素,交换 A,B 位置,去掉B 中所有 A拥有的元素,这样既可取出题目所需:

diff(A, B).concat(diff(B, A))

3. 使用 reduce 方法实现多个数组遍历;

4. 从测试用例中发现不能出现重复的数组值返回,所以需要去重,去重函数:

  function delSame(arr) {
return arr.filter(function(val, curindex) {
return curindex == arr.indexOf(val);
});
}

代码

function sym() {
// difference between set A and set B
function diff(A, B) {
return A.filter(function(val) {
return !B.includes(val);
});
} function delSame(arr) {
return arr.filter(function(val, curindex) {
return curindex == arr.indexOf(val);
});
} var argArray = Array.from(arguments);
return delSame(argArray.reduce(function(arr, cur) {
return diff(arr, cur).concat(diff(cur, arr)).sort();
}));
}
// test here
sym([1, 2, 3], [5, 2, 1, 4]);

[Advanced Algorithm] - Symmetric Difference的更多相关文章

  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. Symmetric Difference FreeCodeCamp

    function sym(args) { var arr = Array.prototype.slice.call(arguments); return arr.reduce((arr1, arr2) ...

  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. 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design

    (Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...

  8. [Advanced Algorithm] - Inventory Update

    题目 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物 ...

  9. [Advanced Algorithm] - Exact Change

    题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...

随机推荐

  1. 洛谷 P3496 [POI2010]GIL-Guilds

    P3496 [POI2010]GIL-Guilds 题目描述 King Byteasar faces a serious matter. Two competing trade organisatio ...

  2. servlet和Spring的DispatcherServlet详解

    Servlet是什么 1. Servlet是服务器端运行的一个程序,是一个被编译好的Java类.它不是框架等. 2. Web容器的启动,需要依赖Servlet.当web服务器开始执行时,servlet ...

  3. 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景

    主场景要包括其它类的头文件 #include "cocos2d.h" #include "MyPlane.h" #include "Bullet.h& ...

  4. spring+springmvc+hibernate架构、maven分模块开发样例小项目案例

    maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准 ...

  5. 一句话实现Mysql查询结果带行号

    SELECT @rowno:=@rowno + 1 AS rowno,a.* FROM tableName a,(SELECT @rowno:=0) b

  6. SqlService Date 格式化

    <choose> <when test="dateFlag=='day'"> ), FRI.INVOICE_DATE, ) AS CREATED_ON, & ...

  7. oc40--类的启动过程

    // // main.m // 类的启动过程 #import <Foundation/Foundation.h> #import "Person.h" #import ...

  8. oc30--id

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (void)sleep; @ ...

  9. linux驱动注册汇总

    --- 01)TP file_operations: { 1. static struct file_operations tpd_fops = { // .owner = THIS_MODULE, ...

  10. linux下dd命令详解【转】

    本文转载自:http://www.cnblogs.com/licheng/articles/1116492.html  名称: dd 使用权限: 所有使用者dd 这个指令在 manual 里的定义是 ...