1var twoSum = function(nums, target) {
2 const indicies = {}
3
4 for (let i = 0; i < nums.length; i++) {
5 const num = nums[i]
6 if (indicies[target - num] != null) {
7 return [indicies[target - num], i]
8 }
9 indicies[num] = i
10 }
11};
1var twoSum = function(nums, target) {
2 //hash table
3 var hash = {};
4
5 for(let i=0; i<=nums.length; i++){
6 //current number
7 var currentNumber = nums[i];
8 //difference in the target and current number
9 var requiredNumber = target - currentNumber;
10 // find the difference number from hashTable
11 const index2 = hash[requiredNumber];
12
13 // if number found, return index
14 // it will return undefined if not found
15 if(index2 != undefined) {
16 return [index2, i]
17 } else {
18 // if not number found, we add the number into the hashTable
19 hash[currentNumber] = i;
20
21 }
22 }
23};