Created
September 6, 2017 14:26
-
-
Save travissanon/62f755d810604ac152d0f2cef9be97ae to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithms - Diff Two Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment