re: Daily Challenge #63- Two Sum VIEW POST


reduce is good for this one.
Here's a JS quickie with O(n) complexity.

twoSum = (numbers, target) => numbers.reduce(([result, partials], num, i) => ([
  result || (num in partials && [partials[num], i]), 
  Object.assign(partials, {[target - num]: i})
]), [undefined, {}])[0]

The partials map maps the index of each value to the value required to bring it up to the target. This can then be picked up by a later value to the get the answer.

code of conduct - report abuse