You are viewing a single comment's thread. Return to all 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; }
}
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 →
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; }
}