Sort by

recency

|

6012 Discussions

|

  • + 0 comments

    my java solution, all tests pass.

    public static int sockMerchant(int n, List ar) { // Write your code here Map map = new HashMap<>(); for(int i=0; i< ar.size(); i++) { if(!map.containsKey(ar.get(i))) map.put(ar.get(i), 1); else { map.put(ar.get(i), map.get(ar.get(i))+1); } } int numPairs=0; Iterator i = map.values().iterator(); while(i.hasNext()) { numPairs = numPairs + (int)i.next()/2; } return numPairs; }

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

    def sockMerchant(n, ar): pair = 0

    set1 = list(set(ar))
    for i in set1:
        if ar.count(i) % 2 == 0:
            pair = pair + ar.count(i)/2
        elif ar.count(i)%2 == 1:
            pair = pair+((ar.count(i)-1)/2)
    return int(pair)
    
  • + 0 comments
    def sockMerchant(n, ar):
        # Write your code here
        
        mydict = {}
        
        for i in ar:
            if i in mydict:
                mydict[i] += 1
            else:
                mydict[i] = 1
                
        ans = sum([x//2 for x in mydict.values()])
    
        return ans
    
  • + 0 comments

    Python 3

    def sockMerchant(n, ar):
        r = 0
        
        for i in set(ar):
            r += ar.count(i) // 2
        return r