Skip to content

Instantly share code, notes, and snippets.

@TechWithTy
Created September 15, 2019 01:52
Show Gist options
  • Save TechWithTy/0adf50511c304db47d15d4be34f91259 to your computer and use it in GitHub Desktop.
Save TechWithTy/0adf50511c304db47d15d4be34f91259 to your computer and use it in GitHub Desktop.
HackerRank Sock Pairs Challenge
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