1function sumArray(a, b) {
2 var c = [];
3 for (var i = 0; i < Math.max(a.length, b.length); i++) {
4 c.push((a[i] || 0) + (b[i] || 0));
5 }
6 return c;
7}
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};
1function sumArrays(...arrays) {
2 const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
3 const result = Array.from({ length: n });
4 return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
5}
6
7console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
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};