Last active
January 7, 2020 14:38
-
-
Save veeracs/714299bbc96f502187e0912c1ff41e5c to your computer and use it in GitHub Desktop.
Return the total number of matching pairs of numbers
This file contains 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 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