Skip to content

Instantly share code, notes, and snippets.

@veeracs
Last active January 7, 2020 14:38
Show Gist options
  • Save veeracs/714299bbc96f502187e0912c1ff41e5c to your computer and use it in GitHub Desktop.
Save veeracs/714299bbc96f502187e0912c1ff41e5c to your computer and use it in GitHub Desktop.
Return the total number of matching pairs of numbers
function numberPairs(n, ar) {
if (n !== ar.length) {
console.error('Invalid array length');
}
const obj = {};
const pairs = [];
const pairsLength = 0;
do {
const temp = ar.shift();
if (!obj[temp]) {
obj[temp] = 1;
} else {
obj[temp]++;
if (obj[temp] === 2) {
pairs.push(temp);
obj[temp] = 0;
}
}
} while (ar.length);
console.log(pairs);
console.log(obj);
return pairs.length;
}
numberPairs(9, [10, 20, 20, 10, 10, 30, 50, 10, 20]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment