2021-05-05 10:13:49 -07:00
|
|
|
|
---
|
|
|
|
|
id: a3f503de51cf954ede28891d
|
2021-10-10 13:18:16 -07:00
|
|
|
|
title: 找到對稱差異
|
2021-05-05 10:13:49 -07:00
|
|
|
|
challengeType: 5
|
|
|
|
|
forumTopicId: 301611
|
|
|
|
|
dashedName: find-the-symmetric-difference
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
兩個集合的數學術語 <dfn>對稱差異</dfn> (`△` 或 `⊕`)是指存在於兩個集合中的任意一個集合,但不同時存在於兩個集合中的元素的集。 例如集合 `A = {1, 2, 3}` 和 `B = {2, 3, 4}`,那麼 `A △ B = {1, 4}`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
對稱差異是一種二元操作,這意味着它只能在兩個元素上操作。 所以要評估一個涉及*三個*元素的表達式(`A △ B △ C`),你必須一次完成一個操作。 因此,對於上述 `A` 和 `B` 兩個集合,如果 `C = {2, 3}`,那麼 `A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
創建一個接受兩個或更多數組的函數,並返回所提供數組的對稱差異。 結果數組必須僅包含唯一值( *不重複* )。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3], [5, 2, 1, 4])` 應該返回 `[3, 4, 5]` 。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3], [5, 2, 1, 4])` 應該僅包含三個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3, 3], [5, 2, 1, 4])` 應返回 `[3, 4, 5]` 。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3, 3], [5, 2, 1, 4])` 應僅包含三個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3], [5, 2, 1, 4, 5])` 應該返回 `[3, 4, 5]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 3], [5, 2, 1, 4, 5])` 應僅包含三個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` 應該返回 `[1, 4, 5]` 。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` 應僅包含三個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` 應該返回 `[1, 4, 5]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` 應僅包含三個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` 應該返回 `[2, 3, 4, 6, 7]` 。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```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]
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` 應該只包含五個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(
|
|
|
|
|
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length,
|
|
|
|
|
5
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`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]` 。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```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]
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:18:16 -07:00
|
|
|
|
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])` 應僅包含八個元素。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```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]);
|
|
|
|
|
```
|