Skip to content

Instantly share code, notes, and snippets.

@blessedjasonmwanza
Created February 22, 2022 07:32
Show Gist options
  • Save blessedjasonmwanza/8a632653c6918bbb260343f2c2cb704d to your computer and use it in GitHub Desktop.
Save blessedjasonmwanza/8a632653c6918bbb260343f2c2cb704d to your computer and use it in GitHub Desktop.
Two Sum LeetCode Challenge
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// You can return the answer in any order.
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(let i = 0; i < nums.length; i +=1){
const posB = i + 1;
if(nums[i] + nums[posB] === target){
return [i, posB];
}
}
};
console.log(twoSum([3,2,4, 1], 6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment