Skip to content

Instantly share code, notes, and snippets.

@creotip
Last active July 20, 2021 19:27
Show Gist options
  • Save creotip/91905b9557f264d044e282d3c074f216 to your computer and use it in GitHub Desktop.
Save creotip/91905b9557f264d044e282d3c074f216 to your computer and use it in GitHub Desktop.
hasPairWithSum : Google coding interview solution
function hasPairWithSum(nums, sum) {
const set = new Set()
let sumArr = []
for (let i = 0; i < nums.length; i++) {
const complement = sum - nums[i]
if (set.has(complement)) {
sumArr = [[...set].indexOf(complement), i]
} else {
set.add(nums[i])
}
}
return !!sumArr.length
}
@creotip
Copy link
Author

creotip commented Jul 20, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment