You are viewing a single comment's thread. Return to all comments →
my java solution, all tests pass.
public static int sockMerchant(int n, List ar) { // Write your code here Map map = new HashMap<>(); for(int i=0; i< ar.size(); i++) { if(!map.containsKey(ar.get(i))) map.put(ar.get(i), 1); else { map.put(ar.get(i), map.get(ar.get(i))+1); } } int numPairs=0; Iterator i = map.values().iterator(); while(i.hasNext()) { numPairs = numPairs + (int)i.next()/2; } return numPairs; }
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 →
my java solution, all tests pass.
public static int sockMerchant(int n, List ar) { // Write your code here Map map = new HashMap<>(); for(int i=0; i< ar.size(); i++) { if(!map.containsKey(ar.get(i))) map.put(ar.get(i), 1); else { map.put(ar.get(i), map.get(ar.get(i))+1); } } int numPairs=0; Iterator i = map.values().iterator(); while(i.hasNext()) { numPairs = numPairs + (int)i.next()/2; } return numPairs; }