dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
194 lines
4.4 KiB
Markdown
194 lines
4.4 KiB
Markdown
---
|
|
id: a5de63ebea8dbee56860f4f2
|
|
title: Diff Two Arrays
|
|
challengeType: 1
|
|
forumTopicId: 16008
|
|
dashedName: diff-two-arrays
|
|
---
|
|
|
|
# --description--
|
|
|
|
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
|
|
|
|
**Note:** You can return the array with its elements in any order.
|
|
|
|
# --hints--
|
|
|
|
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` should return an array.
|
|
|
|
```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"]` should return `["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"]` should return an array with one item.
|
|
|
|
```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"]` should return `["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"]` should return an array with two items.
|
|
|
|
```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"]` should return `[]`.
|
|
|
|
```js
|
|
assert.sameMembers(
|
|
diffArray(
|
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
|
),
|
|
[]
|
|
);
|
|
```
|
|
|
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` should return an empty array.
|
|
|
|
```js
|
|
assert(
|
|
diffArray(
|
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
|
).length === 0
|
|
);
|
|
```
|
|
|
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return `[4]`.
|
|
|
|
```js
|
|
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
|
|
```
|
|
|
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return an array with one item.
|
|
|
|
```js
|
|
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
|
|
```
|
|
|
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return `["piglet", 4]`.
|
|
|
|
```js
|
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
|
|
'piglet',
|
|
4
|
|
]);
|
|
```
|
|
|
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return an array with two items.
|
|
|
|
```js
|
|
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
|
|
```
|
|
|
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return `["snuffleupagus", "cookie monster", "elmo"]`.
|
|
|
|
```js
|
|
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
|
|
'snuffleupagus',
|
|
'cookie monster',
|
|
'elmo'
|
|
]);
|
|
```
|
|
|
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return an array with three items.
|
|
|
|
```js
|
|
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
|
|
```
|
|
|
|
`[1, "calf", 3, "piglet"], [7, "filly"]` should return `[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"]` should return an array with six items.
|
|
|
|
```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) {
|
|
if (arr1.length === 0) return arr2;
|
|
if (arr2.length === 0) return arr1;
|
|
|
|
const set1 = new Set(arr1);
|
|
const set2 = new Set(arr2);
|
|
|
|
const newArr = [];
|
|
|
|
set1.forEach(element => {
|
|
if (!set2.has(element)) newArr.push(element);
|
|
|
|
});
|
|
|
|
set2.forEach(element => {
|
|
if (!set1.has(element)) newArr.push(element);
|
|
|
|
});
|
|
|
|
return newArr;
|
|
|
|
}
|
|
|
|
```
|