Sort by

recency

|

5994 Discussions

|

  • + 0 comments
       public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
        int count = 0;
        for(int i=0; i<ar.size(); i++) {
            for(int j=i+1; j<ar.size(); j++) {
                if(ar.get(j)==-1) {
                    continue;
                }
                if(ar.get(i)==ar.get(j)) {
                    count++;
                    ar.set(j,-1);
                    break;
                }
            }
        }
        return count;
    
        }
    
    }
    
  • + 0 comments

    Solution C#:

        public static int sockMerchant(int n, List<int> ar)
                {
                        int pairCount = 0; 
                        var sockDict = new Dictionary<int , int>();
                        foreach(var item in ar){
                                if(sockDict.ContainsKey(item)){
                                        sockDict[item] += 1;
                                }
                                else {
                                        sockDict[item] = 1;
                                }
                        }
    
                        foreach(var kvp in sockDict){
                                pairCount+= (int)kvp.Value/2;
                        }
    
                        return pairCount;
                }
    
  • + 0 comments
        public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
            Map<Integer, Integer> map = new HashMap<>();
                
            ar.stream().forEach(val -> {
                if (map.containsKey(val)) {
                    map.remove(val);
                } else {
                    map.put(val, 1);
                }
            });
            
            return (ar.size() - map.keySet().size()) / 2;
        }
    
  • + 0 comments
    // Javascript
    function sockMerchant(n, ar) {
        let pairCount = 0;
        let i = 0;
        ar.sort((a,b) => a - b);
        while(i < n - 1){
            if(ar[i] == ar[i+1]){
                pairCount++;
                i+=2;
            }
            else {
                i++;
            }
        }
        return pairCount;
    }
    
  • + 0 comments
    def sockMerchant(n, ar):
        # Write your code here
        count=0
        dict_values={}
        for socks in ar:
            if socks in dict_values:
                dict_values[socks]+=1
            else:
                dict_values[socks]=1
        for socks,pairs in dict_values.items():
            if pairs>=2:
                count+=pairs//2
        return count