191 lines
4.6 KiB
Markdown
191 lines
4.6 KiB
Markdown
![]() |
---
|
||
|
id: a5de63ebea8dbee56860f4f2
|
||
|
title: Differenza di due array
|
||
|
challengeType: 5
|
||
|
forumTopicId: 16008
|
||
|
dashedName: diff-two-arrays
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
Confronta due array e restituisci un nuovo array con tutti gli elementi trovati in uno dei due array dati, ma non in entrambi. In altre parole, restituisce la differenza simmetrica dei due array.
|
||
|
|
||
|
**Nota:** Puoi restituire l'array con i suoi elementi in qualsiasi ordine.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` dovrebbe restituire un 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"]` dovrebbe restituire `["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"]` dovrebbe restituire un array con un elemento.
|
||
|
|
||
|
```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"]` dovrebbe restituire `["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"]` dovrebbe restituire un array con due elementi.
|
||
|
|
||
|
```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"]` dovrebbe restituire `[]`.
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
),
|
||
|
[]
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` dovrebbe restituire un array vuoto.
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
diffArray(
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||
|
['andesite', 'grass', 'dirt', 'dead shrub']
|
||
|
).length === 0
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` dovrebbe restituire `[4]`.
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
|
||
|
```
|
||
|
|
||
|
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` dovrebbe restituire un array con un elemento.
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` dovrebbe restituire `["piglet", 4]`.
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
|
||
|
'piglet',
|
||
|
4
|
||
|
]);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` dovrebbe restituire un array con due elementi.
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
|
||
|
```
|
||
|
|
||
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` dovrebbe restituire `["snuffleupagus", "cookie monster", "elmo"]`.
|
||
|
|
||
|
```js
|
||
|
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
|
||
|
'snuffleupagus',
|
||
|
'cookie monster',
|
||
|
'elmo'
|
||
|
]);
|
||
|
```
|
||
|
|
||
|
`[], ["snuffleupagus", "cookie monster", "elmo"]` dovrebbe restituire un array con tre elementi.
|
||
|
|
||
|
```js
|
||
|
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
|
||
|
```
|
||
|
|
||
|
`[1, "calf", 3, "piglet"], [7, "filly"]` dovrebbe restituire `[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"]` dovrebbe restituire un array con sei elementi.
|
||
|
|
||
|
```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;
|
||
|
}
|
||
|
```
|