• + 0 comments

    Trying my best without Map and List

    public static int sockMerchant(int n, List<Integer> ar) {
            // 1 2 3 4 1 2 3 4
            int totalPairs = 0;
            int[] colorDistinct = new int[0];
            for (int i = 0; i < n; i++) {
                int sock = ar.get(i);
                int pair = 0;
                for (int j = i; j < n; j++) {
                    if (ar.get(j) == sock && !isColorCounted(sock, colorDistinct)) {
                        pair++;
                    }
                }
                totalPairs += pair / 2;
                colorDistinct = addColor(sock, colorDistinct);
            }
            return totalPairs;
        }
        
        private static boolean isColorCounted(int sock, int[] colorDistinct) {
            for (int color : colorDistinct) {
                if (color == sock) {
                    return true;
                }
            }
            return false;
        }
        
        private static int[] addColor(int sock, int[] colorDistinct) {
            int[] newDistinct = new int[colorDistinct.length + 1];
            for (int i = 0; i < colorDistinct.length; i++) {
                newDistinct[i] = colorDistinct[i];
            }
            newDistinct[colorDistinct.length] = sock;
            return newDistinct;
        }