Symmetric Difference

创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(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}).

 

重点:

  1. 找两数组之间互相都没有的数   组成新的数组就是需要得到的值   如:【2,3,4】【4,5,6】   得 【2,3,5,6】
  2. 若参数数组有3或者n个  , 则在已得到两个数组的结果【2,3,5,6】 与下一个数组比较  再得到新的数组

代码如下:

function sym(args) {
  var arr = [];
  for (var i = 0; i < arguments.length; i++) {
      arr.push(arguments[i]);
  }
  /* 找两数组之间差异的数 【2,3,4】【3,5,6】  得  【2,4,5,6】*/
  var arg = arr.reduce(function(one ,  two , index , array){
      var a = one.filter(function(d){    //抛出数组1在数组2中找不到的数   给新数组a
          return two.indexOf(d) == -1;
      });
      var b = two.filter(function(d){    //抛出数组2在数组1中找不到的数 给新数组b
          return one.indexOf(d) == -1;
      });
      return a.concat(b);  //将两数组结合,得到我们想要的结果
  });
  // 去重
  arg = arg.filter(function(item,index){
      return arg.indexOf(item) == index;
  });
  return arg;
}

 

转载于:https://www.cnblogs.com/Leon-Huang-everyone/p/SymmetricDifference-FCC-hht.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(i) To show that X △ Y = (X ∪ Y ) − (X &cap; Y ), we need to show that every element in X △ Y is in (X ∪ Y ) − (X &cap; Y ) and vice versa. Let x be an element in X △ Y. Then, x is either in X - Y or Y - X. If x is in X - Y, then x is in X ∪ Y and not in X &cap; Y. Therefore, x is in (X ∪ Y ) − (X &cap; Y ). Similarly, if x is in Y - X, then x is in X ∪ Y and not in X &cap; Y, so x is in (X ∪ Y ) − (X &cap; Y ). Therefore, every element in X △ Y is in (X ∪ Y ) − (X &cap; Y ). Now, let x be an element in (X ∪ Y ) − (X &cap; Y ). Then, x is in either X or Y but not both. Without loss of generality, assume x is in X. Then, x is not in X &cap; Y, so x is not in Y. Therefore, x is in X - Y, which means that x is in X △ Y. Similarly, if x is in Y but not X, then x is in Y - X and hence in X △ Y. Therefore, every element in (X ∪ Y ) − (X &cap; Y ) is in X △ Y. Hence, we have shown that X △ Y = (X ∪ Y ) − (X &cap; Y ). (ii) To show that (M − X) △ (M − Y ) = X △ Y, we need to show that every element in (M − X) △ (M − Y ) is in X △ Y and vice versa. Let x be an element in (M − X) △ (M − Y ). Then, x is either in (M − X) - (M − Y ) or in (M − Y ) - (M − X). If x is in (M − X) - (M − Y ), then x is in Y but not X. Therefore, x is in X △ Y. Similarly, if x is in (M − Y ) - (M − X), then x is in X but not Y, and hence x is in X △ Y. Therefore, every element in (M − X) △ (M − Y ) is in X △ Y. Now, let x be an element in X △ Y. Then, x is either in X - Y or Y - X. If x is in X - Y, then x is in (M − Y ) - (M − X), so x is in (M − X) △ (M − Y ). Similarly, if x is in Y - X, then x is in (M − X) - (M − Y ), and hence x is in (M − X) △ (M − Y ). Therefore, every element in X △ Y is in (M − X) △ (M − Y ). Hence, we have shown that (M − X) △ (M − Y ) = X △ Y. (iii) To show that the symmetric difference is associative, i.e., (X △ Y ) △ Z = X △ (Y △ Z), we need to show that every element in (X △ Y ) △ Z is in X △ (Y △ Z) and vice versa. Let x be an element in (X △ Y ) △ Z. Then, x is either in (X - Y) - Z or in (Y - X) - Z or in Z - (X △ Y ). If x is in (X - Y) - Z, then x is in X △ (Y △ Z) since x is in X but not in Y △ Z. Similarly, if x is in (Y - X) - Z, then x is in X △ (Y △ Z) since x is in Y but not in X or Z. Finally, if x is in Z - (X △ Y ), then x is either in Z - X and not in Y or in Z - Y and not in X. In the former case, x is in X △ (Y △ Z) since x is in X but not in Y △ Z. In the latter case, x is in X △ (Y △ Z) since x is in Y but not in X or Z. Therefore, every element in (X △ Y ) △ Z is in X △ (Y △ Z). Now, let x be an element in X △ (Y △ Z). Then, x is either in X - (Y △ Z) or in (Y △ Z) - X. If x is in X - (Y △ Z), then x is either in X - Y or in X - Z. Without loss of generality, assume x is in X - Y. Then, x is in (X - Y) - Z and hence in (X △ Y ) △ Z. Similarly, if x is in (Y △ Z) - X, then x is either in Y - X or in Z - X. Without loss of generality, assume x is in Y - X. Then, x is in (Y - X) - Z and hence in (X △ Y ) △ Z. Therefore, every element in X △ (Y △ Z) is in (X △ Y ) △ Z. Hence, we have shown that the symmetric difference is associative. (iv) To show that X &cap; (Y △ Z) = (X &cap; Y ) △ (X &cap; Z), we need to show that every element in X &cap; (Y △ Z) is in (X &cap; Y ) △ (X &cap; Z) and vice versa. Let x be an element in X &cap; (Y △ Z). Then, x is in X and x is in Y or x is in Z but not both. Without loss of generality, assume x is in Y but not Z. Then, x is in X &cap; Y but not in X &cap; Z, so x is in (X &cap; Y ) △ (X &cap; Z). Therefore, every element in X &cap; (Y △ Z) is in (X &cap; Y ) △ (X &cap; Z). Now, let x be an element in (X &cap; Y ) △ (X &cap; Z). Then, x is either in (X &cap; Y) - (X &cap; Z) or in (X &cap; Z) - (X &cap; Y). Without loss of generality, assume x is in (X &cap; Y) - (X &cap; Z). Then, x is in X and x is in Y but not in Z, so x is in X &cap; (Y △ Z). Therefore, every element in (X &cap; Y ) △ (X &cap; Z) is in X &cap; (Y △ Z). Hence, we have shown that X &cap; (Y △ Z) = (X &cap; Y ) △ (X &cap; Z). (v) To show that X △ Y = Z △ W iff X △ Z = Y △ W, we need to show that if X △ Y = Z △ W, then X △ Z = Y △ W and vice versa. Assume that X △ Y = Z △ W. Then, (X - Y) ∪ (Y - X) = (Z - W) ∪ (W - Z) (X - Y) ∪ (Y - X) = (Z &cap; W') ∪ (W &cap; Z') (X - Y) ∪ (Y - X) = (Z ∪ W) - (Z &cap; W) - (W &cap; Z) + (Z &cap; W) (X ∪ Z') &cap; (Y ∪ W') ∪ (X' ∪ Z) &cap; (Y' ∪ W) = (X ∪ W') &cap; (Y ∪ Z') ∪ (X' ∪ W) &cap; (Y' ∪ Z) Now, let's simplify the left-hand side of this equation. We have: (X ∪ Z') &cap; (Y ∪ W') ∪ (X' ∪ Z) &cap; (Y' ∪ W) = [(X ∪ Z') &cap; (X' ∪ Z)] ∪ [(X ∪ Z') &cap; (Y' ∪ W)] ∪ [(Y ∪ W') &cap; (X' ∪ Z)] ∪ [(Y ∪ W') &cap; (Y' ∪ W)] = [(X &cap; Z') ∪ (X' &cap; Z)] ∪ [(X &cap; Y') ∪ (Z' &cap; W)] ∪ [(Y &cap; X') ∪ (W' &cap; Z)] ∪ [(Y &cap; W') ∪ (Y' &cap; W)] Similarly, we can simplify the right-hand side of the equation: (X ∪ W') &cap; (Y ∪ Z') ∪ (X' ∪ W) &cap; (Y' ∪ Z) = [(X &cap; W') ∪ (X' &cap; W)] ∪ [(X &cap; Z') ∪ (Y' &cap; Z)] ∪ [(Y &cap; W') ∪ (X' &cap; Y)] ∪ [(Y &cap; Z') ∪ (Y' &cap; Z)] Now, we can see that the equation holds if and only if every term on the left-hand side appears exactly once on the right-hand side. For example, the term (X &cap; Z') appears on the left-hand side but not on the right-hand side, so it must cancel out with another term that appears on the right-hand side but not on the left-hand side. By inspecting the two simplified expressions, we can see that this is indeed the case. Therefore, X △ Z = Y △ W. Conversely, assume that X △ Z = Y △ W. Then, using a similar argument as above, we can show that X △ Y = Z △ W. Hence, we have shown that X △ Y = Z △ W iff X △ Z = Y △ W. (vi) The region of X △ Y △ Z in a Venn diagram is the region that is shaded in exactly one of the three circles X, Y, Z. (vii) A sketch of a Venn diagram for 4 distinct sets is shown below: ``` A B o-----o-----o |\ | /| | \ | / | C o--\--o--/--o D | \ | / | | \|/ | o-----o-----o E F ``` In this diagram, the regions A, B, C, D, E, F, AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD, and ABCD represent the different subsets of the four sets. The region that is shaded in exactly one of the four circles represents the symmetric difference of those sets.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值