Files
freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/algorithms/find-the-symmetric-difference.md

151 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: a3f503de51cf954ede28891d
title: 找到对称差异
challengeType: 5
forumTopicId: 301611
dashedName: find-the-symmetric-difference
---
# --description--
两个集合的数学术语 <dfn>对称差异</dfn> `△``⊕`)是指存在于两个集合中的任意一个集合,但不同时存在于两个集合中的元素的集。 例如集合 `A = {1, 2, 3}``B = {2, 3, 4}`,那么 `A △ B = {1, 4}`
对称差异是一种二元操作,这意味着它只能在两个元素上操作。 所以要评估一个涉及*三个*元素的表达式(`A △ B △ C`),你必须一次完成一个操作。 因此,对于上述 `A``B` 两个集合,如果 `C = {2, 3}`,那么 `A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}`
# --instructions--
创建一个接受两个或更多数组的函数,并返回所提供数组的对称差异。 结果数组必须仅包含唯一值( *不重复* )。
# --hints--
`sym([1, 2, 3], [5, 2, 1, 4])` 应该返回 `[3, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
```
`sym([1, 2, 3], [5, 2, 1, 4])` 应该仅包含三个元素。
```js
assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
```
`sym([1, 2, 3, 3], [5, 2, 1, 4])` 应返回 `[3, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
```
`sym([1, 2, 3, 3], [5, 2, 1, 4])` 应仅包含三个元素。
```js
assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
```
`sym([1, 2, 3], [5, 2, 1, 4, 5])` 应该返回 `[3, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
```
`sym([1, 2, 3], [5, 2, 1, 4, 5])` 应仅包含三个元素。
```js
assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
```
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` 应该返回 `[1, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
```
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` 应仅包含三个元素。
```js
assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
```
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` 应该返回 `[1, 4, 5]`
```js
assert.sameMembers(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])` 应仅包含三个元素。
```js
assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
```
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` 应该返回 `[2, 3, 4, 6, 7]`
```js
assert.sameMembers(
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])` 应该只包含五个元素。
```js
assert.equal(
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length,
5
);
```
`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]`
```js
assert.sameMembers(
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])` 应仅包含八个元素。
```js
assert.equal(
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
.length,
8
);
```
# --seed--
## --seed-contents--
```js
function sym(args) {
return args;
}
sym([1, 2, 3], [5, 2, 1, 4]);
```
# --solutions--
```js
function sym() {
var arrays = [].slice.call(arguments);
return arrays.reduce(function (symDiff, arr) {
return symDiff.concat(arr).filter(function (val, idx, theArr) {
return theArr.indexOf(val) === idx
&& (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);
});
});
}
sym([1, 2, 3], [5, 2, 1, 4]);
```