How to modify or delete items from an enumerable collection while iterating through it in C#

Iterating Backwards through the List sounds like a better approach, because if you remove an element and other elements “fall into the gap”, that does not matter because you have already looked at those. Also, you do not have to worry about your counter variable becoming larger than the .Count. List<int> test = new List<int>(); …

Read more

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

There’s nothing wrong with direct, “ad-hoc” solution. It can be clean enough too. Just notice that spiral is built from segments. And you can get next segment from current one rotating it by 90 degrees. And each two rotations, length of segment grows by 1. edit Illustration, those segments numbered … 11 10 7 7 …

Read more