Skip to content

Instantly share code, notes, and snippets.

@leyanlo
Created November 30, 2020 08:21
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 leyanlo/3457d6f259ede4fa6fa56989745ec4ee to your computer and use it in GitHub Desktop.
Save leyanlo/3457d6f259ede4fa6fa56989745ec4ee to your computer and use it in GitHub Desktop.
cassidoo 11/29/2020
function arrayDiff(arr, target) {
const seen = {};
return arr.reduce((numPairs, n) => {
const targetsFromN = { [n + target]: null, [n - target]: null };
numPairs += Object.keys(targetsFromN).reduce(
(seenFromN, targetFromN) => seenFromN + (seen[targetFromN] || 0),
0
);
seen[n] = (seen[n] || 0) + 1;
return numPairs;
}, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment