190 lines
5.2 KiB
Markdown
190 lines
5.2 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"]` потрібно повернути масив з одним елементом.
|
|||
|
|
|||
|
```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"]` потрібно повернути масив з двома елементами.
|
|||
|
|
|||
|
```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]` потрібно повернути масив з одним елементом.
|
|||
|
|
|||
|
```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]` потрібно повернути масив з двома елементами.
|
|||
|
|
|||
|
```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"]` потрібно повернути масив з трьома елементами.
|
|||
|
|
|||
|
```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"]` потрібно повернути масив з шістьма елементами.
|
|||
|
|
|||
|
```js
|
|||
|
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
|
|||
|
```
|
|||
|
|
|||
|
# --seed--
|
|||
|
|
|||
|
## --seed-contents--
|
|||
|
|
|||
|
```js
|
|||
|
function diffArray(arr1, arr2) {
|
|||
|
const newArr = [];
|
|||
|
return newArr;
|
|||
|
}
|
|||
|
|
|||
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
|||
|
```
|
|||
|
|
|||
|
# --solutions--
|
|||
|
|
|||
|
```js
|
|||
|
function diffArray(arr1, arr2) {
|
|||
|
const newArr = [];
|
|||
|
const h1 = Object.create(null);
|
|||
|
arr1.forEach(function(e) {
|
|||
|
h1[e] = e;
|
|||
|
});
|
|||
|
const 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;
|
|||
|
}
|
|||
|
```
|