freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../intermediate-algorithm-scri.../diff-two-arrays.md

4.6 KiB

id title challengeType forumTopicId dashedName
a5de63ebea8dbee56860f4f2 Differenza di due array 5 16008 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.

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"].

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.

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"].

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.

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 [].

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.

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].

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.

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].

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.

assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);

[], ["snuffleupagus", "cookie monster", "elmo"] dovrebbe restituire ["snuffleupagus", "cookie monster", "elmo"].

assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
  'snuffleupagus',
  'cookie monster',
  'elmo'
]);

[], ["snuffleupagus", "cookie monster", "elmo"] dovrebbe restituire un array con tre elementi.

assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);

[1, "calf", 3, "piglet"], [7, "filly"] dovrebbe restituire [1, "calf", 3, "piglet", 7, "filly"].

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.

assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);

--seed--

--seed-contents--

function diffArray(arr1, arr2) {
  const newArr = [];
  return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

--solutions--

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;
}