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

4.5 KiB

id title challengeType forumTopicId dashedName
a5de63ebea8dbee56860f4f2 Diferenciar Dois Arrays 5 16008 diff-two-arrays

--description--

Compare dois arrays e retorne um novo array com qualquer item encontrado em apenas um dos dois arrays passados, mas não ambos. Em outras palavras, retorne a diferença simétrica de dois arrays.

Nota: Você pode retornar o array com seus elementos em qualquer ordem.

--hints--

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) deve retornar um 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"] deve retornar ["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"] deve retornar um array com um item.

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"] deve retornar ["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"] deve retornar um array com dois itens.

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"] deve retornar [].

assert.sameMembers(
  diffArray(
    ['andesite', 'grass', 'dirt', 'dead shrub'],
    ['andesite', 'grass', 'dirt', 'dead shrub']
  ),
  []
);

["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] deve retornar um array vazio.

assert(
  diffArray(
    ['andesite', 'grass', 'dirt', 'dead shrub'],
    ['andesite', 'grass', 'dirt', 'dead shrub']
  ).length === 0
);

[1, 2, 3, 5], [1, 2, 3, 4, 5] deve retornar [4].

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

[1, 2, 3, 5], [1, 2, 3, 4, 5] deve retornar um array com um item.

assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] deve retornar ["piglet", 4].

assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
  'piglet',
  4
]);

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] deve retornar um array com dois itens.

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

[], ["snuffleupagus", "cookie monster", "elmo"] deve retornar ["snuffleupagus", "cookie monster", "elmo"].

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

[], ["snuffleupagus", "cookie monster", "elmo"] deve retornar um array com três itens.

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

[1, "calf", 3, "piglet"], [7, "filly"] deve retornar [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"] deve retornar um array com seis itens.

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

--seed--

--seed-contents--

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

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

--solutions--

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