Skip to content

Instantly share code, notes, and snippets.

@misterussell
Created November 21, 2016 15:31
Show Gist options
  • Save misterussell/868a7b0c4960f6746541f73e9c743465 to your computer and use it in GitHub Desktop.
Save misterussell/868a7b0c4960f6746541f73e9c743465 to your computer and use it in GitHub Desktop.
Function to return unique values from two arrays
function diff(arr1, arr2) {
// put the arrays together
// loop over the data
combinedArr = arr1.concat(arr2);
// filter over the combined array and compare the index and last index of each number
// if they match then return that number
let combedArray = combinedArr.filter((num) => {
if (combinedArr.indexOf(num) === combinedArr.lastIndexOf(num)) {
return num;
}
});
return combedArray;
};
arr1 = [1,6,4];
arr2 = [3,1,2,4,9];
console.log(diff(arr1, arr2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment