Created
September 15, 2019 01:52
-
-
Save TechWithTy/0adf50511c304db47d15d4be34f91259 to your computer and use it in GitHub Desktop.
HackerRank Sock Pairs Challenge
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 sortAndCount( n, arr ) { | |
let sorted = arr.sort( (a,b) => a - b); | |
let pairs = 0; | |
for (let i = 0; i < n -1 ; i++) { | |
if ( sorted[i] === sorted[i + 1]) { | |
pairs++; | |
i += 1; | |
} | |
} | |
return pairs; | |
} | |
function sockMerchant(n, ar) { | |
const colors = {} | |
let pairs = 0; | |
for (const color of ar) | |
if (colors[color]) { | |
colors[color] = 0; | |
pairs += 1; | |
} else { | |
colors[color] = 1; | |
} | |
return pairs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment