code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

02 Two Number Sum

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const twoNumberSum = (nums, target) => {
  let map = {}
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i]
    if (map[complement]) {
      return true
    } else {
      map[nums[i]] = nums[i]
    }
  }
  return false
}

console.log(twoNumberSum([3, 5, -4, 8, 11, 1, -1, 6], 10))

Created 2021-12-31T12:42:25+00:00 · Edit