travissanon
9/6/2017 - 2:26 PM

freeCodeCamp Intermediate Algorithms - Diff Two Arrays

freeCodeCamp Intermediate Algorithms - Diff Two Arrays

function diffArray(arr1, arr2) {
  var sorted1 = arr1.filter(function(e){
    return this.indexOf(e) < 0;
  }, arr2);
  var sorted2 = arr2.filter(function(e){
    return this.indexOf(e) < 0;
  }, arr1);
  // Same, same; but different.
  return sorted1.concat(sorted2);
}

diffArray([1, "calf", 3, "piglet"], [7, "filly"]);


// Test parameters
// diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
// diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
// diffArray([], ["snuffleupagus", "cookie monster", "elmo"]);