Created
September 26, 2020 08:18
-
-
Save muhsalaa/36c14c2e2031e3018395300b6d7c2d51 to your computer and use it in GitHub Desktop.
find sock pairs between socks array
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 sockMerchant(n, ar) { | |
// create color hash | |
const colors = {}; | |
// count total matches (pairs) | |
let matches = 0; | |
ar.forEach(x => { | |
// if color with truthy value exist, increment pairs by 1 and set color to zero to make it falsy | |
if (colors[x]) { | |
matches++; | |
colors[x] = 0 | |
} | |
// if color value is falsy (0 or have no key inside hash), create or add 1 | |
else { | |
colors[x] = 1 | |
} | |
}) | |
// result of socks pair | |
return matches | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment