Skip to content

Instantly share code, notes, and snippets.

@dashsaurabh
Created January 18, 2020 10:39
Show Gist options
  • Select an option

  • Save dashsaurabh/6269344fe0cf518e67afc6aa2dc42e92 to your computer and use it in GitHub Desktop.

Select an option

Save dashsaurabh/6269344fe0cf518e67afc6aa2dc42e92 to your computer and use it in GitHub Desktop.
Find the Indices of Two Numbers From an Array Whose Sum is Equal to the Target
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let lookUp = {}
for(let i = 0; i < nums.length; i++) {
let complement = target - nums[i];
if(lookUp[complement] !== undefined){
return [lookUp[complement], i]
}
lookUp[nums[i]] = i;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment