We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Typescript solution. Time O(n), Space O(k). k is the number of colors.
function sockMerchant(n: number, ar: number[]): number {
if (n < 2) return 0;
const unpairedSocks = new Set<number>();
let pairs = 0;
for (const color of ar) {
if (unpairedSocks.has(color)) {
pairs++;
unpairedSocks.delete(color);
} else {
unpairedSocks.add(color);
}
}
return pairs;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sales by Match
You are viewing a single comment's thread. Return to all comments →
Typescript solution. Time O(n), Space O(k). k is the number of colors.
function sockMerchant(n: number, ar: number[]): number { if (n < 2) return 0;
}