Skip to content

Instantly share code, notes, and snippets.

@okovalov
Created February 25, 2019 05:22
Show Gist options
  • Save okovalov/4362b7ae4470c7fe3415a992e27c0416 to your computer and use it in GitHub Desktop.
Save okovalov/4362b7ae4470c7fe3415a992e27c0416 to your computer and use it in GitHub Desktop.
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