Last active
October 19, 2018 03:48
-
-
Save manivelarjunan/2df77d3eb40bf0ddefdb31d8df107aaa to your computer and use it in GitHub Desktop.
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
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