You are viewing a single comment's thread. Return to all 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; }
Seems like cookies are disabled on this browser, please enable them to open this website
Sales by Match
You are viewing a single comment's thread. Return to all comments →
Java stack solution: