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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Sales by Match
  5. Discussions

Sales by Match

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 5662 Discussions, By:

recency

Please Login in order to post a comment

  • bredy452
    1 day ago+ 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|
    Permalink
  • neershil67
    1 day ago+ 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|
    Permalink
  • Gotanod
    2 days ago+ 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|
    Permalink
  • agokselb
    4 days ago+ 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;
    }
    
    0|
    Permalink
  • jeeumpearson
    5 days ago+ 1 comment

    Pythonic solution:

    def sockMerchant(n, ar):
        return sum([ar.count(color)//2 for color in set(ar)])
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy