Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created October 10, 2017 16:16
Show Gist options
  • Save jwill9999/1df2b5aaf84d63192ffe2f25f35a595f to your computer and use it in GitHub Desktop.
Save jwill9999/1df2b5aaf84d63192ffe2f25f35a595f to your computer and use it in GitHub Desktop.
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.
*/
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