Sort by

recency

|

5986 Discussions

|

  • + 0 comments

    Python drawer = {}

    for sock in ar:
        drawer[sock] = drawer.get(sock, 0) + 1
    
    single_pair = sum(1 if x % 2 != 0 else 0 for x in drawer.values())
    matching_pair = int((len(ar) - single_pair)/2)
    
    return matching_pair
    
  • + 0 comments

    Here is my c++ solution , you can find the video explanation here : https://youtu.be/HPIhFXx_DVM

    int sockMerchant(int n, vector<int> ar) {
        map<int, int>mp;
        int result = 0;
        for(int i = 0; i < ar.size(); i++){
            if(mp[ar[i]]){
                result ++;
                mp.erase(ar[i]);
                continue;
            }
            mp[ar[i]]++;
        }
        return result;
    }
    
  • + 0 comments
    public static int sockMerchant(int n, List<Integer> ar) {
        int[] count = new int[101];
        int pairs = 0;
    
        for(int i = 0; i < ar.size(); i++) {
            count[ar.get(i)]++;
        }
    
        for(int i = 1; i < 101; i++) {
            pairs += count[i]/2;
        }
    
        return pairs;
    }****
    
  • + 0 comments

    As a Linq lover, I would just do this

    public static int sockMerchant(int n, List<int> ar)
    {
        var ret = ar.GroupBy(x => x).Select(x => new {
            item = x.Key,
            freq = x.Count() / 2
        }).Sum(x => x.freq);
        return ret;
    }`
    
  • + 0 comments

    Easily Understandable Approach

    public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
        
        Set<Integer> set = new HashSet<>();
        int c=0;
    
        for(Integer i:ar){
            if(set.contains(i)){
                set.remove(i);
                c++;
            }
            else{
                set.add(i);
            }
        }
    
        return c;
        }