Beginner is here. I have a String ArrayList that essentially looks like this (but with varying values ββdepending on user input) when printing:
[22, 37, 77, 77, 98, 101, 104, 107, 107, 107, 150]
I want to remove duplicate elements and add the total number of occurrences in parentheses after the first element, so it will look like this:
[22, 37, 77 (2), 98, 101, 104, 107 (3), 150]
I figured out how to remove duplicate items, but I can't figure out everything else.
Here is my code so far (ArrayList called duplication):
int q, z; for(q = 0; q < duplicates.size() - 1; q++) { for(z = q + 1; z < duplicates.size() - 1; z++) { if(duplicates.get(q).equals(duplicates.get(z))) { duplicates.remove(q); } } } System.out.println(duplicates);
Resulting result:
[22, 37, 77, 98, 101, 104, 107, 150]
Does anyone have any suggestions on how I can get these parentheses with the number of occurrences there? I struggled to come up with a way to remove duplicates for each value, but all I managed to calculate was the total number of deleted periods, which is not particularly useful.
ArrayList was originally an Integer ArrayList, but I changed it to a String ArrayList, so I could add non-numbers to elements.