• + 0 comments

    Java stack solution:

        public static int sockMerchant(int n, List<Integer> ar) {
            Collections.sort(ar);
            int counter = 0;
            Stack<Integer> stack = new Stack<>();
            
            for (Integer value : ar) {
                if(stack.empty()){
                    stack.add(value);
                } else {
                    if(stack.peek() == value){
                        stack.pop();
                        counter++;
                    } else {    
                        stack.add(value);
                    }
                }
            }
            return counter;
        }