Skip to content

Instantly share code, notes, and snippets.

@muhsalaa
Created September 26, 2020 08:18
Show Gist options
  • Save muhsalaa/36c14c2e2031e3018395300b6d7c2d51 to your computer and use it in GitHub Desktop.
Save muhsalaa/36c14c2e2031e3018395300b6d7c2d51 to your computer and use it in GitHub Desktop.
find sock pairs between socks array
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