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.
Sales by Match
Sales by Match
+ 0 comments JS Solution
let matchMap = {} let pairs = 0 ar.forEach(num => { if (matchMap[num] === undefined) { matchMap[num] = 0 } matchMap[num] += 1 }) for (const num in matchMap) { if (matchMap[num] > 1) { pairs += (Math.floor(matchMap[num] / 2)) } } return pairs
+ 0 comments C++ solution
int sockMerchant(int n, vector<int> ar) { map<int,int> socks; int pairsock=0; for (int i =0; i<n; i++) { socks[ar[i]]++; } for(auto &pair : socks){ pairsock += pair.second/2; } return pairsock; }
+ 0 comments My C++ solution
int sockMerchant(int n, vector<int> ar) { std::vector<bool> odds = std::vector<bool>(100); int nPairs = 0; for ( int sockID : ar ) { if ( odds[sockID-1] ) { nPairs++; odds[sockID-1] = false; } else { // odd sock found odds[sockID-1] = true; } } return nPairs; }
+ 0 comments Typescript
function sockMerchant(n: number, ar: number[]): number { const socks = ar.sort((a,b)=>a-b); let result = 0; let last = socks[0]; let pair = 0; for(let sock of socks) { if(sock === last) pair++; else { result += pair%2 === 0 ? (pair)/2 : (pair-1)/2; pair = 1; } last = sock; } result += pair%2 === 0 ? (pair)/2 : (pair-1)/2; return result; }
+ 1 comment Pythonic solution:
def sockMerchant(n, ar): return sum([ar.count(color)//2 for color in set(ar)])
Load more conversations
Sort 5662 Discussions, By:
Please Login in order to post a comment