Skip to content

Instantly share code, notes, and snippets.

@carl-parrish
Created December 11, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carl-parrish/231306011542f0b7bd645334b90cf53a to your computer and use it in GitHub Desktop.
Save carl-parrish/231306011542f0b7bd645334b90cf53a to your computer and use it in GitHub Desktop.
Two Sum
function twoSum(arr, target) {
  return arr.reduce((pairs, val, indx, srcArr) => {
    let remainder = target - val;
    let indx2 = srcArr.indexOf(remainder);
    return indx2 !== -1 ? [...pairs, [val, remainder]] : pairs;
  }, []);
}

const numArray = [1, 6, 4, 5, 3, 3];
const sum = 7;

console.log(twoSum(numArray, sum));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment