Sales by Match

Sort by

recency

|

355 Discussions

|

  • + 0 comments
       public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
            var set = new HashSet<>();
            var numberPairs = 0;
            
            for (int sock : ar) {
                if (!set.add(sock)) {
                    set.remove(sock);
                    numberPairs++;
                }
            }
            return numberPairs;
    
        }
    
  • + 0 comments
    #Sorting, and counting stocks with same color, and division is not necesary
    def sockMerchant(n, ar):
        p = defaultdict(bool)
        c=0
        for num in ar:
            if not p[num]:
                p[num] = True
            else:
                c += 1
                p[num] = False
        return c 
    
  • + 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; }

  • + 1 comment
    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