Created
February 9, 2020 23:11
-
-
Save angle943/f6111a6d6f9348b2c9280183e2b3c002 to your computer and use it in GitHub Desktop.
Javascript Freecodecamp Algorithm #30: Pairwise
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 pairwise(arr, arg) { | |
const usedDict = {}; | |
let output = 0; | |
for (let i = 0; i < arr.length - 1; i++) { | |
if (usedDict[i]) { | |
continue; | |
} | |
const iVal = arr[i]; | |
for (let j = i + 1; j < arr.length; j++) { | |
if (usedDict[j]) { | |
continue; | |
} | |
const jVal = arr[j]; | |
if (iVal + jVal === arg) { | |
usedDict[i] = true; | |
usedDict[j] = true; | |
output += i + j; | |
break; | |
} | |
} | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment