Created
February 25, 2019 05:22
-
-
Save okovalov/4362b7ae4470c7fe3415a992e27c0416 to your computer and use it in GitHub Desktop.
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
const twoSum = (arr, num) => { | |
const result = [] | |
const hash = [] | |
for (let i in arr) { | |
const currentNumber = arr[i] | |
const expectedNumber = num - currentNumber | |
if (hash.indexOf(expectedNumber) !== -1) { | |
result.push([currentNumber, expectedNumber]) | |
} | |
if (hash.indexOf(currentNumber) < 0) hash.push(currentNumber) | |
} | |
return result | |
} | |
const num = 7 | |
const arr = [1,9,9,5,6,5,4,5,3,3] // [[6,1],[3,4],[3,4]] | |
const result = twoSum(arr, num) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment