Created
October 1, 2017 08:02
-
-
Save tkssharma/4acaf073ce88160add5686ed64a65dec to your computer and use it in GitHub Desktop.
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
function diffFromArray(array1,array2){ | |
var map = {}; | |
var out = []; | |
if(array1.length == 0 || array2.length === 0 ){ | |
return null | |
}; | |
array1.forEach(function(element){ | |
map[element] = 1; | |
}); | |
array2.forEach(function(element){ | |
if(map[element] === 1){ | |
// do nothing we are good here element exist in both array | |
}else{ | |
out.push(element) | |
} | |
}); | |
return out; | |
} | |
//diffFromArray([1,2,3],[7,2,4,5,6,7]); | |
//(5) [7, 4, 5, 6, 7] | |
//diffFromArray([1,2,3],[1,2,4,5,3]); | |
//(2) [4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment