Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 1, 2017 08:02
Show Gist options
  • Save tkssharma/4acaf073ce88160add5686ed64a65dec to your computer and use it in GitHub Desktop.
Save tkssharma/4acaf073ce88160add5686ed64a65dec to your computer and use it in GitHub Desktop.
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