Edit: I notice that it is possible to use hashset to stop duplicate elements, however, I don't think using that can count the duplicate elements, please correct me if I am wrong.
I'm trying to store items in an array list. Then after storing, scan through for duplicates and mark how many of each unique duplicate there is. I tried to use a nested for loop to scan through an arraylist for each element, and comparing it to every other item, however, it didn't remove duplicates correctly or keep track of the number of duplicates correctly.
ArrayList<Integer> itemQuantity = new ArrayList<Integer>();
for(int i = 0; i < array.size(); i++) {
int quantity = 1;
for(int j = 1; j < array.size(); j++) {
if(array.get(j).equalsIgnoreCase(array.get(i))) {
array.remove(j);
quantity++;
}
}
itemQuantity.add(quantity);
}
This is what I had. I am open to answering any questions about my code and sorry if I'm unclear. This is my first time using this website.
I made arraylist itemQuantity to store the amounts of duplicates for each unique element. Then first for loop is checking through each element, then nested for loop is comparing it to every other element, if an element is the same as another, remove it from the list and increase one to the duplicate count.