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}).


要求

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]) 应该只包含八个元素.

代码

function sym(args) {
  var arr = [];
  for(var i = 0; i < arguments.length; i++) {
    arr.push(unique(arguments[i]));
  } 
  var result = arr.reduce(function(a,b) {
    return sortArr(a,b);
  }); 
  return result;    
}

function unique(arr) {
  arr = arr.reduce(function(array,val){
    if(array.indexOf(val) === -1) {
      array.push(val);
    }
    return array;
  },[]);
  return arr;
}

function sortArr(arr1,arr2) {
  var arr = [];
  arr1.forEach(function(element) {
    if(arr2.indexOf(element) === -1) {
      arr.push(element);
    }
  });
  arr2.forEach(function(element) {
    if(arr1.indexOf(element) === -1) {
      arr.push(element);
    }
  });
  return arr;
}
sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(i) To show that X △ Y = (X ∪ Y ) − (X ∩ Y ), we need to show that every element in X △ Y is in (X ∪ Y ) − (X ∩ 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 ∩ Y. Therefore, x is in (X ∪ Y ) − (X ∩ Y ). Similarly, if x is in Y - X, then x is in X ∪ Y and not in X ∩ Y, so x is in (X ∪ Y ) − (X ∩ Y ). Therefore, every element in X △ Y is in (X ∪ Y ) − (X ∩ Y ). Now, let x be an element in (X ∪ Y ) − (X ∩ 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 ∩ 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 ∩ Y ) is in X △ Y. Hence, we have shown that X △ Y = (X ∪ Y ) − (X ∩ 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 ∩ (Y △ Z) = (X ∩ Y ) △ (X ∩ Z), we need to show that every element in X ∩ (Y △ Z) is in (X ∩ Y ) △ (X ∩ Z) and vice versa. Let x be an element in X ∩ (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 ∩ Y but not in X ∩ Z, so x is in (X ∩ Y ) △ (X ∩ Z). Therefore, every element in X ∩ (Y △ Z) is in (X ∩ Y ) △ (X ∩ Z). Now, let x be an element in (X ∩ Y ) △ (X ∩ Z). Then, x is either in (X ∩ Y) - (X ∩ Z) or in (X ∩ Z) - (X ∩ Y). Without loss of generality, assume x is in (X ∩ Y) - (X ∩ Z). Then, x is in X and x is in Y but not in Z, so x is in X ∩ (Y △ Z). Therefore, every element in (X ∩ Y ) △ (X ∩ Z) is in X ∩ (Y △ Z). Hence, we have shown that X ∩ (Y △ Z) = (X ∩ Y ) △ (X ∩ 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 ∩ W') ∪ (W ∩ Z') (X - Y) ∪ (Y - X) = (Z ∪ W) - (Z ∩ W) - (W ∩ Z) + (Z ∩ W) (X ∪ Z') ∩ (Y ∪ W') ∪ (X' ∪ Z) ∩ (Y' ∪ W) = (X ∪ W') ∩ (Y ∪ Z') ∪ (X' ∪ W) ∩ (Y' ∪ Z) Now, let's simplify the left-hand side of this equation. We have: (X ∪ Z') ∩ (Y ∪ W') ∪ (X' ∪ Z) ∩ (Y' ∪ W) = [(X ∪ Z') ∩ (X' ∪ Z)] ∪ [(X ∪ Z') ∩ (Y' ∪ W)] ∪ [(Y ∪ W') ∩ (X' ∪ Z)] ∪ [(Y ∪ W') ∩ (Y' ∪ W)] = [(X ∩ Z') ∪ (X' ∩ Z)] ∪ [(X ∩ Y') ∪ (Z' ∩ W)] ∪ [(Y ∩ X') ∪ (W' ∩ Z)] ∪ [(Y ∩ W') ∪ (Y' ∩ W)] Similarly, we can simplify the right-hand side of the equation: (X ∪ W') ∩ (Y ∪ Z') ∪ (X' ∪ W) ∩ (Y' ∪ Z) = [(X ∩ W') ∪ (X' ∩ W)] ∪ [(X ∩ Z') ∪ (Y' ∩ Z)] ∪ [(Y ∩ W') ∪ (X' ∩ Y)] ∪ [(Y ∩ Z') ∪ (Y' ∩ 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 ∩ 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、付费专栏及课程。

余额充值