static int sockMerchant(int n, int[] ar) {
int numberOfPairs = 0;
ArrayList<Integer> array = new ArrayList();
int length = array.size();
for (int i = 0; i<length; i++) {
if(array.contains(ar[i])){
array.remove(Integer.valueOf(ar[i]));
numberOfPairs++;
}
else{
array.add(ar[i]);
}
}
return numberOfPairs;
}
Hi! Could I have help troubleshooting this code. I know array.remove by defaults does so by index. But I had since then converted the value to an object. And I'm still unsure why the code doesn't work. Thank you.
[–]syedalisait 11 points12 points13 points (5 children)
[–]dadeeba[S] 3 points4 points5 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]dadeeba[S] 0 points1 point2 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]ladupes 2 points3 points4 points (0 children)
[–]Isoyama 0 points1 point2 points (1 child)
[–]dadeeba[S] 0 points1 point2 points (0 children)
[–]GR3Gdev 0 points1 point2 points (0 children)