Skip to content

Instantly share code, notes, and snippets.

@nkhil
Last active October 17, 2020 16:49
Show Gist options
  • Save nkhil/a465b97708bb52c5807d5cf7834b87f2 to your computer and use it in GitHub Desktop.
Save nkhil/a465b97708bb52c5807d5cf7834b87f2 to your computer and use it in GitHub Desktop.

Sum of Pairs

Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum. CodeWars kata link

Note: The following solution is not by me. It was one of the first solutions in the showcase under 'Solutions'. Apologies for not attributing the authors.

var sum_pairs = function (ints, s) {
  var seen = {}
  for (var i = 0; i < ints.length; ++i) {
    if (seen[s - ints[i]]) return [s - ints[i], ints[i]];
    seen[ints[i]] = true
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment