Sales by Match

Sort by

recency

|

356 Discussions

|

  • + 0 comments
    int sockMerchant(int n, vector<int> ar) {
        int pair = 0;
        unordered_map<int, int> map;
        for(size_t i = 0; i < ar.size(); i++){
            map[ar[i]]++;
        }
        for(auto it: map){
            pair += (it.second / 2);
        }
        return pair;
    }
    
  • + 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; }