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));
Created
December 11, 2017 12:45
-
-
Save carl-parrish/231306011542f0b7bd645334b90cf53a to your computer and use it in GitHub Desktop.
Two Sum
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment