Removing items from a list when you're iterating through the list causes problems because you're causing the list to shift around at the same time that the loop is trying to keep track of its position.
Suppose you have a list of Fruits: Apple, Banana, Banana, Cherry, Date. Suppose you want to remove Banana. So you write code to do this:
for each fruit in Fruits
if fruit is Banana
remove fruit
Here's one possible scenario, depending on how iteration is implemented by the programming language or library: First, the code points to element 0 (assuming zero-based indexing), where it finds Apple. Apple doesn't match Banana. So the code moves on to the next item in the list, in position 1: Banana. Banana matches Banana, so that Banana is removed.
Now the list Fruits consists of Apple, Banana, Cherry, Date. The second Banana is sitting at position 1, where the first Banana had been. The Cherry is now sitting where the second Banana had been, in position 2; and the Date is sitting where Cherry had been, in position 3.
Since the code has already done a comparison and deletion at position 1, it isn't going to do another one! It's time for it to increment, to move on to position 2, where it finds Cherry. This means that it has skipped over the second Banana. When the loop completes, you'll still have that second Banana there. The list will be Apple, Banana, Cherry, Date.
An approach that may work, depending on how arrays are implemented (including whether they are dynamic and you can remove elements from them without causing the remaining elements to be resequenced), is to create an array from the elements in the list, and then iterate through them by index, in turn, from n-1 through 0 (assuming zero-based arrays). You have to do it in reverse order or else you'll run into the same problem as before. When you iterate in reverse order, a deletion causes the elements you've already visited to shift, but not the elements you haven't visited yet.