Created
January 13, 2022 16:53
-
-
Save Sanchithasharma/61443ddedc13746df66a7596f90b48b0 to your computer and use it in GitHub Desktop.
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
// 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. | |
// Note: You can return the array with its elements in any order. | |
function diffArray(arr1, arr2) { | |
const newArr = []; | |
filter(arr1, arr2, newArr) | |
filter(arr2, arr1, newArr) | |
return newArr; | |
} | |
function filter(arr1, arr2, newArr) { | |
arr1.map(el => { | |
if(!arr2.includes(el)) { | |
newArr.push(el) | |
} | |
}) | |
} | |
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); |
Author
Sanchithasharma
commented
Jan 13, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment