Sales by Match

Sort by

recency

|

353 Discussions

|

  • + 0 comments

    `

    int sockMerchant(int n, vector ar) { std::sort(ar.begin(),ar.end()); int pairs = 0;

    for (int i = 0; i < n - 1;) {
        if (ar[i] == ar[i + 1]) {
            pairs++;
            i += 2; 
        } else {
            i += 1; 
        }
    }    
    return pairs;
    

    }

    `

  • + 0 comments

    C++ with basic code: int sockMerchant(int n, vector ar) { int n_pair = 0; int j = 0; stable_sort(ar.begin(), ar.end()); for (int i = 0; i < n; i++) { if ((ar[i + j] == ar[i+j+1]) && (j + i + 1 < n)) { n_pair += 1; j += 1; } } return n_pair; }

  • + 0 comments
    from collections import Counter
    def sockMerchant(n, ar):
        # Write your code here
        count = 0
        socks = Counter(ar)
        for key, value in socks.items():
            if value >= 2:
                count += value//2
        
        return count 
    
  • + 0 comments

    from collections import Counter

    def sockMerchant(n, ar):

    count = Counter(ar)
    res = 0
    
    val = list(count.values())
    
    for i in val:
    
        if i >= 2:
            res += i//2
    
    return res
    
  • + 0 comments

    c++

    int sockMerchant(int n, vector<int> ar) {
        unordered_map<int, int> frequencyMap;
        
        int nPairs = 0;
        
        for(int i = 0; i < ar.size(); ++i){
            frequencyMap[ar[i]] += 1;
            if(frequencyMap[ar[i]] % 2 == 0){
                nPairs +=1;
            }
        }
        
        return nPairs;
    }