191 lines
4.5 KiB
Markdown
191 lines
4.5 KiB
Markdown
|
---
|
||
|
id: a5de63ebea8dbee56860f4f2
|
||
|
title: 數組的對稱差
|
||
|
challengeType: 5
|
||
|
forumTopicId: 16008
|
||
|
dashedName: diff-two-arrays
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
比較兩個數組並返回一個新數組,包含所有隻在其中一個數組中出現的元素,排除兩個數組都存在的元素。 換言之,我們需要返回兩個數組的對稱差。
|
||
|
|
||
|
**注意:**返回數組中的元素順序不會影響挑戰的測試結果。
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` 應返回一個數組。
|
||
|
|
||
|
```js
|
||
|
assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === 'object');
|
||
|
```
|
||
|
|
||
|
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 應返回 `["pink wool"]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(
|
||
|
diffArray(
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
),
|
||
|
['pink wool']
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 應返回一個長度爲 1 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
diffArray(
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
).length === 1
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 應返回 `["diorite", "pink wool"]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
),
|
||
|
['diorite', 'pink wool']
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 應返回一個長度爲 2 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
).length === 2
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` 應返回 `[]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
),
|
||
|
[]
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` 應返回一個空數組。
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
).length === 0
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` 應返回 `[4]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
|
||
|
```
|
||
|
|
||
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` 應返回一個長度爲 1 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` 應返回 `["piglet", 4]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
|
||
|
'piglet',
|
||
|
4
|
||
|
]);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` 應返回一個長度爲 2 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
|
||
|
```
|
||
|
|
||
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` 應返回 `["snuffleupagus", "cookie monster", "elmo"]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
|
||
|
'snuffleupagus',
|
||
|
'cookie monster',
|
||
|
'elmo'
|
||
|
]);
|
||
|
```
|
||
|
|
||
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` 應返回一個長度爲 3 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [7, "filly"]` 應返回 `[1, "calf", 3, "piglet", 7, "filly"]`。
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']), [
|
||
|
1,
|
||
|
'calf',
|
||
|
3,
|
||
|
'piglet',
|
||
|
7,
|
||
|
'filly'
|
||
|
]);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [7, "filly"]` 應返回一個長度爲 6 的數組。
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```js
|
||
|
function diffArray(arr1, arr2) {
|
||
|
var newArr = [];
|
||
|
return newArr;
|
||
|
}
|
||
|
|
||
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```js
|
||
|
function diffArray(arr1, arr2) {
|
||
|
var newArr = [];
|
||
|
var h1 = Object.create(null);
|
||
|
arr1.forEach(function(e) {
|
||
|
h1[e] = e;
|
||
|
});
|
||
|
|
||
|
var h2 = Object.create(null);
|
||
|
arr2.forEach(function(e) {
|
||
|
h2[e] = e;
|
||
|
});
|
||
|
|
||
|
Object.keys(h1).forEach(function(e) {
|
||
|
if (!(e in h2)) newArr.push(h1[e]);
|
||
|
});
|
||
|
Object.keys(h2).forEach(function(e) {
|
||
|
if (!(e in h1)) newArr.push(h2[e]);
|
||
|
});
|
||
|
return newArr;
|
||
|
}
|
||
|
```
|