let twoSum = (arr) => { return compliment Arr } // TEST Arr const nums Arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2] code example
Example: two sum javascript
var twoSum = function(nums, target) {
//hash table
var hash = {};
for(let i=0; i<=nums.length; i++){
//current number
var currentNumber = nums[i];
//difference in the target and current number
var requiredNumber = target - currentNumber;
// find the difference number from hashTable
const index2 = hash[requiredNumber];
// if number found, return index
// it will return undefined if not found
if(index2 != undefined) {
return [index2, i]
} else {
// if not number found, we add the number into the hashTable
hash[currentNumber] = i;
}
}
};