2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: a5de63ebea8dbee56860f4f2
|
2021-03-14 21:20:39 -06:00
|
|
|
title: 数组的对称差
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
forumTopicId: 16008
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: diff-two-arrays
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
比较两个数组并返回一个新数组,包含所有只在其中一个数组中出现的元素,排除两个数组都存在的元素。 换言之,我们需要返回两个数组的对称差。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
**注意:**返回数组中的元素顺序不会影响挑战的测试结果。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` 应返回一个数组。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === 'object');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 应返回 `["pink wool"]`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.sameMembers(
|
|
|
|
diffArray(
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
),
|
|
|
|
['pink wool']
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 应返回一个长度为 1 的数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
diffArray(
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
).length === 1
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 应返回 `["diorite", "pink wool"]`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.sameMembers(
|
|
|
|
diffArray(
|
|
|
|
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
),
|
|
|
|
['diorite', 'pink wool']
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` 应返回一个长度为 2 的数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
diffArray(
|
|
|
|
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
|
|
|
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
).length === 2
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` 应返回 `[]`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.sameMembers(
|
|
|
|
diffArray(
|
|
|
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
|
|
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` 应返回一个空数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
diffArray(
|
|
|
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
|
|
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
|
|
|
).length === 0
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` 应返回 `[4]`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` 应返回一个长度为 1 的数组。
|
2020-09-07 16:10:29 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` 应返回 `["piglet", 4]`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
|
|
|
|
'piglet',
|
|
|
|
4
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` 应返回一个长度为 2 的数组。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` 应返回 `["snuffleupagus", "cookie monster", "elmo"]`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
|
|
|
|
'snuffleupagus',
|
|
|
|
'cookie monster',
|
|
|
|
'elmo'
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` 应返回一个长度为 3 的数组。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, "calf", 3, "piglet"], [7, "filly"]` 应返回 `[1, "calf", 3, "piglet", 7, "filly"]`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']), [
|
|
|
|
1,
|
|
|
|
'calf',
|
|
|
|
3,
|
|
|
|
'piglet',
|
|
|
|
7,
|
|
|
|
'filly'
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`[1, "calf", 3, "piglet"], [7, "filly"]` 应返回一个长度为 6 的数组。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
|
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function diffArray(arr1, arr2) {
|
2021-10-27 15:10:57 +00:00
|
|
|
const newArr = [];
|
2021-01-13 03:31:00 +01:00
|
|
|
return newArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
function diffArray(arr1, arr2) {
|
2021-10-27 15:10:57 +00:00
|
|
|
const newArr = [];
|
|
|
|
const h1 = Object.create(null);
|
2021-01-13 03:31:00 +01:00
|
|
|
arr1.forEach(function(e) {
|
|
|
|
h1[e] = e;
|
|
|
|
});
|
2021-10-27 15:10:57 +00:00
|
|
|
const h2 = Object.create(null);
|
2021-01-13 03:31:00 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
```
|