-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-three.js
More file actions
27 lines (24 loc) · 831 Bytes
/
script-three.js
File metadata and controls
27 lines (24 loc) · 831 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
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const numArrOne = [2, 7, 11, 8, 14, 6], targetOne = 9; // [0, 1]
const numArrTwo = [3, 4, 2, 8, 15, 20], targetTwo = 6; // [1, 2]
const numArrThree = [16, 22, 7, 4, 8], targetThree = 12; // [3, 4]
const numArrFour = [2, 5, 5, 6, 11], targetFour = 10; // [1, 2]
const numArrFive = [3, 3], targetFive = 6; // [0, 1]
// missing duplicate index check
const twoSum = (nums, target) => {
const out = [];
for (let i = 0; i < nums.length; i++) {
let dif = target - nums[i];
if (nums.includes(dif)) {
out.push(i, nums.indexOf(dif));
return out;
}
}
};
console.log('numArrOne', twoSum(numArrOne, targetOne));
console.log('numArrTwo', twoSum(numArrTwo, targetTwo));
console.log('numArrThree', twoSum(numArrThree, targetThree));