Skip to content

Instantly share code, notes, and snippets.

@Constantiner
Last active October 22, 2017 13:39
Show Gist options
  • Select an option

  • Save Constantiner/aea9c6f4e9935722f0315e5773a95933 to your computer and use it in GitHub Desktop.

Select an option

Save Constantiner/aea9c6f4e9935722f0315e5773a95933 to your computer and use it in GitHub Desktop.
const getPairs = (arr, x) => arr.reduce(({pairs, subarray}, element) => ({pairs,
subarray: (subarray.forEach(elem => element + elem === x ? pairs.push([element, elem]) : elem), subarray.slice(1))}),
{pairs: [], subarray: arr.slice(1)}).pairs;
const arr = [ 3, 4, 5, -2, 10, 11, 12, -1, 0, 7, 8 ],
x = 10;
console.log(getPairs(arr, x));
@Constantiner
Copy link
Author

A sample solution for a simple problem of finding pairs in array of integers which sum is equal to some integer value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment