Diff Two Arrays

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

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) should return an array.

["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["pink wool"].

["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with one item.

["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["diorite", "pink wool"].

["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with two items.

["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return [].

["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return an empty array.

[1, 2, 3, 5], [1, 2, 3, 4, 5] should return [4].

[1, 2, 3, 5], [1, 2, 3, 4, 5] should return an array with one item.

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return an array with two items.

[], ["snuffleupagus", "cookie monster", "elmo"] should return ["snuffleupagus", "cookie monster", "elmo"].

[], ["snuffleupagus", "cookie monster", "elmo"] should return an array with three items.

[1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"].

[1, "calf", 3, "piglet"], [7, "filly"] should return an array with six items.

声明式

  1. function diffArray(arr1, arr2) {
  2. return arr1.concat(arr2).filter(x =>
  3. !(arr1.some(y => x === y) && arr2.some(y => x === y)))
  4. }
  5. console.log(
  6. diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])
  7. )
  1. function diffArray(arr1, arr2) {
  2. return arr1
  3. .concat(arr2)
  4. .filter(item => !arr1.includes(item) || !arr2.includes(item));
  5. }
  6. diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

答案

https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-diff-two-arrays/16008

拒绝命令式

  1. function diffArray(arr1, arr2) {
  2. var newArr = [];
  3. function onlyInFirst(first, second) {
  4. // Looping through an array to find elements that don't exist in another array
  5. for (var i = 0; i < first.length; i++) {
  6. if (second.indexOf(first[i]) === -1) {
  7. // Pushing the elements unique to first to newArr
  8. newArr.push(first[i]);
  9. }
  10. }
  11. }
  12. onlyInFirst(arr1, arr2);
  13. onlyInFirst(arr2, arr1);
  14. return newArr;
  15. }
  16. diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

https://devdocs.io/javascript/global_objects/array/includes

https://devdocs.io/javascript/global_objects/array/filter

https://devdocs.io/javascript/global_objects/array/concat