Created
February 8, 2022 18:32
-
-
Save kousherAlam/a899b8592ea088bec5823af29efdce89 to your computer and use it in GitHub Desktop.
This file contains 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 check(numbers:number[], value: number): number[] { | |
const needMap:any = {}; | |
const result: number[] = []; | |
let round = 0; | |
numbers.every((n, index) => { | |
round ++; | |
if(needMap[n] > -1) { | |
result.push(...[numbers[needMap[n]], n]); | |
return false; | |
} else { | |
const needed = value - n ; | |
needMap[needed] = index; | |
} | |
return true; | |
}); | |
console.log(`totoal round ${round} of ${numbers.length}`); | |
return result; | |
} | |
console.log(check([11,2, 5, 1, 4,5,2,302, 7, 3], 10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment