Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Created December 19, 2019 21:33
Show Gist options
  • Save joelbarbosa/dd8a0a87518d9846aa2dbff38163ce51 to your computer and use it in GitHub Desktop.
Save joelbarbosa/dd8a0a87518d9846aa2dbff38163ce51 to your computer and use it in GitHub Desktop.
// Return the total number of matching pairs of socks that John can sell.
// 9
// 10 20 20 10 10 30 50 10 20
// output
// 3
function sockMerchant(n, ar) {
let totalPair=0;
const pairs = new Set();
for (let i=0; i <= n; i++) {
if (!pairs.has(ar[i])) {
pairs.add(ar[i]);
} else {
totalPair++;
pairs.delete(ar[i]);
}
}
return totalPair;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment