• + 0 comments

    In java 8:

    public static int sockMerchant(int n, List ar) { // Write your code here Integer pairs = 0; int i = 0; while(i < (ar.size() - 1)) { Integer sock = ar.get(i); for(int j = i+1; j < ar.size(); j++){ if(sock == ar.get(j)){ pairs++; ar.remove(i); ar.remove(j - 1); break; }

                if(j == (ar.size() - 1)){
                    ar.remove(i);
                }
            }
        }
        return pairs;   
    }
    

    }