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 7719919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:32:07+00:00 2026-06-01T03:32:07+00:00

I have following code to remove group from collection. Technically, there should be no

  • 0

I have following code to remove group from collection. Technically, there should be no duplicates, but it does remove all anyway. Any trick with LINQ to .Remove.Where.. ?

public void DeleteGroup(KeyValuePair<int, string> group)
            {
                while (this.Groups.Any(g => g.Key.Equals(group.Key)))
                {
                    var groupToRemove = this.Groups.First(g => g.Key.Equals(group.Key));
                    this.Groups.Remove(groupToRemove);
                }

            }
  • 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-06-01T03:32:08+00:00Added an answer on June 1, 2026 at 3:32 am

    Assuming you are passing in a KeyValuePair with the same Key and the same Value this is the most efficient way possible with an ObseravableCollection.

    public void DeleteGroup2(KeyValuePair<int, string> group)
    {
        Groups.Remove(group);
    }
    

    This works because a KeyValuePair is a structure and when the overloaded operator == is applied it is comparing both the Key and the Value data members of the structure.

    Again this will work just fine if you pass in the exact same Key and Value that is contained in the Groups obserabableCollection…if the Value does not match it will not work.

    Behind the scenes an ObserableCollection is pretty much a list so it will have to iterate over every item performing the == operator. The same is true for the code you are posting. Just because it is using LINQ doesn’t mean it’s any more efficient. It’s not like the LINQ where clause is using any indexing like it would be with LINQ to SQL.

    public void DeleteGroup3(KeyValuePair<int, string> groupToDelete)
    {
        var itemsToDelete =
            (
                from g in Groups
                where g.Key == groupToDelete.Key
                select g
            );
    
        foreach (var kv in itemsToDelete)
        {
            Groups.Remove(kv);
        }
    }
    

    This would probably be most efficient method using linq if you want to guarantee that you remove all items even those with duplicate keys.

    public void DeleteGroup4(KeyValuePair<int, string> group)
    {
        List<int> keyIndexes = new List<int>();
        int maxIndex = Groups.Count;
        for (int i = 0; i < maxIndex; i++)
        {
            if (Groups[i].Key == group.Key)
            {
                keyIndexes.Add(i);
            }
        }
    
        int indexOffset = 0;
        foreach (int index in keyIndexes)
        {
            Groups.RemoveAt(index - indexOffset);
            indexOffset++;
        }
    }
    

    This should have the best performance of all of them if you have multiple items with the same key or you don’t know the exact same Key Value pair as the original.

    I believe your DeleteGroup method is BIG O of 2N^2…N for the outer Any while Loop and N for the First and N for the Remove. Take outer Loop times the sum of the inside and you get 2N^2

    DeleteGroup2 is BIG O of N and had the best performance of all of them. The drawback is that you need to know both the Key and the Value not just the Key. It will also only remove the first item it finds. It won’t delete duplicate items with the same Key and the same Value.

    DeleteGroup3 IS BIG O of N + N^2. N for the select. Worse case is that your key is in there N times so N^2 for the removal.

    DeleteGroup4 is BIG O of 2N. N to find the indexes and in worst case if you have all items with the same key then its N to remove each of them as RemoveAtIndex is a Big O of 1. This has the best performance if you only know the Key and you have the possibility of having multiple items with the same Key.

    If you know for a fact that you won’t have duplicate items I would use DeleteGroup2. If you have the possibility of having duplicates DeleteGroup4 should have the best performance.

    On a side note if won’t have duplicates and you don’t necessarily know both the Key and the Value you can still use the best performing option of DeleteGroup2 but create a class called KeyValueIntString with properties of Key and Value. Then overide the IsEquals method so that it only compares the Key property unlike the KeyValue struct that compares both the Key and the Value data members. Then you can use the ObserableCollection.Remove method and not have to worry about knowing the value that is stored. I.E. you could pass in instance of a KeyValueIntString that has the Key set but you don’t have to worry about setting the Value property.

    After commenting I decided to Add best readability method although it does have worse performance. Has a Big O of N^4. N for select, N for ToList, N for ForEach and N for Remove.

    public void DeleteGroup5(KeyValuePair<int, string> groupToDelete)
    {
        (
            from g in Groups
            where g.Key == groupToDelete.Key
            select g
        ).ToList().ForEach(g => Groups.Remove(g));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, that should remove the access of users from a
I have written the following code to remove vowels from a sentence: main =
I have following string task BLABLA@{taskId} @{BLABLA.title} and want to extract all placeholders from
I have the following code to generate a table listing brand/model/submodel/style for a group
I have the following code example taken from the code of a Form :
I am practicing how to find and remove dead code. I have the following
I have program which reads MSMQ using GetAllMessages but it does not remove messages
I have the following code to try to remove non-numbers froma string: (apply str
when I want to remove div element, I have the following code: <div class='text-field'>
How does the following code remove window borders? //note the struct is declared elsewhere,

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.