Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:36:24+00:00 2026-05-28T01:36:24+00:00

For now, the best I could think of is: bool oneMoreTime = true; while

  • 0

For now, the best I could think of is:

bool oneMoreTime = true;
while (oneMoreTime)
{
    ItemType toDelete=null;
    oneMoreTime=false;
    foreach (ItemType item in collection)
    {
        if (ShouldBeDeleted(item))
        {
            toDelete=item;
            break;
        }
    }
    if (toDelete!=null)
    {
        collection.Remove(toDelete);
        oneMoreTime=true;
    }
}

I know that I have at least one extra variable here, but I included it to improve the readability of the algorithm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:36:24+00:00Added an answer on May 28, 2026 at 1:36 am

    The “RemoveAll” method is best.

    Another common technique is:

    var itemsToBeDeleted = collection.Where(i=>ShouldBeDeleted(i)).ToList();
    foreach(var itemToBeDeleted in itemsToBeDeleted)
        collection.Remove(itemToBeDeleted);
    

    Another common technique is to use a “for” loop, but make sure you go backwards:

    for (int i = collection.Count - 1; i >= 0; --i)
        if (ShouldBeDeleted(collection[i]))
            collection.RemoveAt(i);
    

    Another common technique is to add the items that are not being removed to a new collection:

    var newCollection = new List<whatever>();
    foreach(var item in collection.Where(i=>!ShouldBeDeleted(i))
        newCollection.Add(item);
    

    And now you have two collections. A technique I particularly like if you want to end up with two collections is to use immutable data structures. With an immutable data structure, “removing” an item does not change the data structure; it gives you back a new data structure (that re-uses bits from the old one, if possible) that does not have the item you removed. With immutable data structures you are not modifying the thing you’re iterating over, so there’s no problem:

    var newCollection = oldCollection;
    foreach(var item in oldCollection.Where(i=>ShouldBeDeleted(i))
        newCollection = newCollection.Remove(item);
    

    or

    var newCollection = ImmutableCollection<whatever>.Empty;
    foreach(var item in oldCollection.Where(i=>!ShouldBeDeleted(i))
        newCollection = newCollection.Add(item);
    

    And when you’re done, you have two collections. The new one has the items removed, the old one is the same as it ever was.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What the best books available right now on Mac and iPhone development ? Thanks
Now I am trying to use MPI_Sendand MPI_Recv to pass best found solutions among
What is the best way to install WinDBG now? It use to be very
What are the best Ruby / Rails book available right now ? Note :
Or the concepts/best practices used there are now deprecated? I'm just starting to use
SO now that the new ipad is on the market what's the best practice
What is best practice to stream multiple files in one ASIHTTPRequest? Right now, for
I'm looking for the best solution for caching thousands of web pages. Right now
I'm wondering how to best tackle this, since what I have now works great
What would be the best database design for employee clocking and out? Right now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.