Skip to content

Instantly share code, notes, and snippets.

@manivelarjunan
Last active October 19, 2018 03:48
Show Gist options
  • Save manivelarjunan/2df77d3eb40bf0ddefdb31d8df107aaa to your computer and use it in GitHub Desktop.
Save manivelarjunan/2df77d3eb40bf0ddefdb31d8df107aaa to your computer and use it in GitHub Desktop.
function sumOfTwoNum(array,target){
let storeMap = {};
for(let arr of array){
if(storeMap[arr]){
return true;
}
storeMap[target - arr] = arr;
}
return false;
}
console.log(sumOfTwoNum([10,15,3,7],18));
// 1st iteration storeMap = {8:10} - check whether key 10 is exist in the map already (if(storeMap[arr]))
// 2nd iteration storeMap = {8:10,3:15} - check whether key 15 is exist in the map already (if(storeMap[arr]))
// 3rd iteration storeMap = {8:10,3:15} - check whether key 3 is exist in the map already (if(storeMap[arr]))
// yes 3 is exist in the map,so 3 + 15(current element) = 18 return true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment