Last active
July 20, 2021 19:27
-
-
Save creotip/91905b9557f264d044e282d3c074f216 to your computer and use it in GitHub Desktop.
hasPairWithSum : Google coding interview solution
This file contains hidden or 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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the video:
https://www.youtube.com/watch?v=XKu_SEDAykw