-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtwo_sum.js
More file actions
30 lines (27 loc) · 705 Bytes
/
two_sum.js
File metadata and controls
30 lines (27 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @author Anirudh Sharma
*/
var twoSum = function (nums, target) {
// Array to store the result
result = [];
// Map to store the difference and its index
index_map = new Map();
// Loop for each element in the array
for (let i = 0; i < nums.length; i++) {
let difference = target - nums[i];
if (index_map.has(difference)) {
result[0] = i;
result[1] = index_map.get(difference);
break;
} else {
index_map.set(nums[i], i);
}
}
return result;
};
let nums = [2, 7, 11, 15];
let target = 9;
console.log(twoSum(nums, target));
nums = [3, 2, 4];
target = 6;
console.log(twoSum(nums, target));