Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

two sum javascript

const nums = [3, 4, 5, 6, 7, 3]
const target = 6

const twoSum = function(nums, target) {
    let map = {}

    for (let i = 0; i < nums.length; i++) {
        if (target - nums[i] in map) {
            return [map[target - nums[i]], i]
        } else {
            map[nums[i]] = i
        }
    }

    return []
}

console.log(twoSum(nums, target))   // [Log]: [ 0, 5 ]
 
PREVIOUS NEXT
Tagged: #sum #javascript
ADD COMMENT
Topic
Name
3+4 =