Cor Ligthert said:
Feel free to use even 100 loops if you want. I think that one short loop
bottom up through the index is never to beat.
"Never to beat"? In what respect? In programming, there are far too many
competing goals for anyone to say one solution is the best one 100% of the
time.
I think it unlikely that if you are iterating through an entire indexed
collection and removing some subset of that collection, you'll see a
significant difference between various means of iteration, in the most
common cases.
If you're dealing with an array, and each removal requires that the
remaining elements in the array be copied down, then yes...starting at the
end will result in a slightly better performance. However, unless you're
deleting most of the members of the array, you won't notice this difference.
The total data moved will only be a little less that way, and it ignores the
benefit of code clarity.
If performance is an issue, then the right way is to simply copy the
references to be preserved to a new array, and then just delete the entire
original. This method will perform WAY better than either a top-down or
bottom-up in-place removal. If performance is not an issue, then you can't
use performance as the sole measure of which method is better.
If you're dealing with a linked list, then removal by index is going to
perform poorly whichever end you start at, and there will be better
performance starting at the beginning (top-down) than the end (bottom-up).
A better way than using an index from either end would be to iterate through
the list by reference, so that removal operations have constant order.
[...]
This method is however not from me. Armin Zingler came with it I thought
about 3 years ago in the Visual Basic newsgroup, where all what is showed
now in this newsgroup was showed before that.
I hate to tell you this, but the question of removing elements from an
indexed array is so old, it's unlikely anyone could say who "came with it".
Neither your method nor mine is from either of us, nor originally from Armin
Zingler (whoever he is) and that fact goes without saying. These concepts
predate .NET, Windows, and the PC.
Now nobody active in the Visual Basic newsgroup will think on other than
bottom up methods for these problems, while they did before this top down,
with all those work around methods showed here.
What work-around methods? The correct top-down method is equivalent to the
bottom-up method, and carries with it no greater degree of "work-around"
than the bottom-up method.
If "nobody active in the Visual Basic newsgroup will think on other than
bottom up methods for these problems", then that's because nobody in that
newsgroup understands that in programming, one size does not fit all. In
reality, I doubt your assertion is true, but if it is, it's only proof of
one-dimensional thinking, rather than the validity of your claim that one
method is better than the other.
Pete