Skip to content

Instantly share code, notes, and snippets.

@jslnriot
Last active December 20, 2022 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jslnriot/d9b80d76fc738512f4235f54464df375 to your computer and use it in GitHub Desktop.
Save jslnriot/d9b80d76fc738512f4235f54464df375 to your computer and use it in GitHub Desktop.
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