Last active
December 20, 2022 01:51
-
-
Save jslnriot/d9b80d76fc738512f4235f54464df375 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
const twoSum = (dataSet, target) => { | |
const nums = {} | |
// Can also use Map() and will work the same | |
// const nums = new Map() | |
for (const num of dataSet) { | |
const potentialMatch = target - num | |
console.log(nums) | |
if(potentialMatch in nums) { | |
return [potentialMatch, num] // found match so return current num + potentialMatch | |
} else { | |
nums[num] = true // add to hash | |
} | |
} | |
return [] | |
} | |
const dataSet = [1,5,22,9,25,2] | |
const target = 11 | |
const results = twoSum(dataSet, target) | |
console.log('Results .... ', results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment