Created
October 10, 2017 16:16
-
-
Save jwill9999/1df2b5aaf84d63192ffe2f25f35a595f to your computer and use it in GitHub Desktop.
Diff two arrays
This file contains hidden or 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
/* | |
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. | |
*/ | |
function diffArray(arr1, arr2) { | |
var newArr = []; | |
function check(first, second) { | |
for (var i = 0; i < first.length; i++) { | |
if (second.indexOf(first[i]) === -1) { | |
newArr.push(first[i]); | |
} | |
} | |
} | |
check(arr1, arr2); | |
check(arr2, arr1); | |
return newArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment