• + 0 comments

    This is my solution using C#.

    public static int sockMerchant(int n, List<int> ar)
    {
    		Dictionary<int, int> itemCounts = new Dictionary<int, int>();
    		int pairCount = 0;
    
    		for (int i = 0; i < n; i++) {
    				if (itemCounts.ContainsKey(ar[i])) {
    						itemCounts[ar[i]]++;
    				} else {
    						itemCounts[ar[i]] = 1;
    				}
    		}
    
    		foreach(var item in itemCounts) {
    				pairCount += (item.Value / 2);
    		}
    
    		return pairCount;
    }
    

    Solution for Python3

    ` def sockMerchant(n, ar): # Write your code here arCount = Counter(ar) pairCount = 0

    for item in arCount:
        pairCount += arCount[item] // 2
    
    return pairCount
    

    `