fork download
  1.  
  2. // your code goes here
  3. function twoSum(nums, target) {
  4. let hashTable = {};
  5. for (let i = 0; i < nums.length; i++) {
  6. let num = nums[i];
  7. let complement = target - num;
  8. if (hashTable[complement] !== undefined) {
  9. return [hashTable[complement], i];
  10. }
  11. hashTable[num] = i;
  12. }
  13. return [];
  14. }
  15.  
  16. // Example usage
  17. const nums = [2, 7, 11, 15];
  18. const target = 9;
  19. console.log(twoSum(nums, target)); // Output: [0, 1]
  20.  
Success #stdin #stdout 0.03s 16676KB
stdin
Standard input is empty
stdout
0,1