Skip to content

Instantly share code, notes, and snippets.

@angle943
Created February 9, 2020 23:11
Show Gist options
  • Save angle943/f6111a6d6f9348b2c9280183e2b3c002 to your computer and use it in GitHub Desktop.
Save angle943/f6111a6d6f9348b2c9280183e2b3c002 to your computer and use it in GitHub Desktop.
Javascript Freecodecamp Algorithm #30: Pairwise
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