Смотрите сами
// list is [1, 2, 2, 3], remove every 2
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 2) { list.remove(i); }
}
// result: [1, 2, 3] — one 2 survived!
After removing the first 2, the second 2 slid into its place — but i had already moved past it.
The fix: walk backwards. Removals then shift only the part you have already visited:
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 2) { list.remove(i); }
}
// result: [1, 3]