Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. //input = array, number
  2. //output = array
  3.  
  4. //test case
  5. //[2, 7, 11, 15], 13
  6. //[0, 2]
  7.  
  8. function twoSum (array, number) {
  9. var solutionObject = {};
  10. for (let i = 0; i < array.length; i++) {
  11. const target = number - array[i];
  12. if (target in solutionObject) {
  13. return [solutionObject[target], i];
  14. } else {
  15. solutionObject[array[i]] = i;
  16. }
  17. }
  18. }
  19.  
  20. twoSum([4, 5, 10], 14);
Add Comment
Please, Sign In to add comment