Skip to content

Instantly share code, notes, and snippets.

@vinnymac
Created September 16, 2019 20:39
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 vinnymac/c0b2d04d93427f6605fcdca0cc07692a to your computer and use it in GitHub Desktop.
Save vinnymac/c0b2d04d93427f6605fcdca0cc07692a to your computer and use it in GitHub Desktop.
Daily Coding Problem #1

Problem 1

Given a list of numbers, return whether any two sums to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Bonus: Can you do this in one pass?

function checkSums(numbers, k) {
const possibleSolutions = new Set()
for (let number of numbers) {
if (possibleSolutions.has(number)) return true
possibleSolutions.add(k - number)
}
return false
}
// checkSums([10, 15, 3, 7], 17) -> true
// checkSums([1, 2, 3, 4], 8) -> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment