Created
November 30, 2020 08:21
-
-
Save leyanlo/3457d6f259ede4fa6fa56989745ec4ee to your computer and use it in GitHub Desktop.
cassidoo 11/29/2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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