• + 0 comments

    This is my Java solution, feel free to ask me any questions.

    public static int sockMerchant(int n, List<Integer> ar) {
        Map<Integer, Integer> sockMap = new HashMap<>();
        
        //Perform counting socks
        for(int sock : ar) {
            int frequency = sockMap.getOrDefault(sock, 0);
            sockMap.put(sock, frequency + 1);
        }
        
        int pairs = 0;
        for(int frequency : sockMap.values()) {
            if(frequency >= 2) pairs += frequency/2;
        }
        
        return pairs;
    }