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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:02:03+00:00 2026-06-03T22:02:03+00:00

I have a rather specific question about how to exclude items of one list

  • 0

I have a rather specific question about how to exclude items of one list from another. Common approaches such as Except() won’t do and here is why:

  • If the duplicate within a list has an “even” index – I need to remove THIS element and the NEXT element AFTER it.
  • if the duplicate within a list had an “odd” index – I need to remove THIS element AND one element BEFORE** it.
  • there might be many appearances of the same duplicate within a list. i.e. one might be with an “odd” index, another – “even”.

I’m not asking for a solution since I’ve created one myself. However after performing this method many times – “ANTS performance profiler” shows that the method elapses 75% of whole execution time (30 seconds out of 40). The question is: Is there a faster method to perform the same operation? I’ve tried to optimize my current code but it still lacks performance. Here it is:

private void removedoubles(List<int> exclude, List<int> listcopy)
{
    for (int j = 0; j < exclude.Count(); j++)
    {
        for (int i = 0; i < listcopy.Count(); i++)
        {
            if (listcopy[i] == exclude[j])
            {
                if (i % 2 == 0) // even
                {
                    //listcopy.RemoveRange(i, i + 1);
                    listcopy.RemoveAt(i);
                    listcopy.RemoveAt(i);
                    i = i - 1;
                }
                else //odd
                {
                    //listcopy.RemoveRange(i - 1, i);
                    listcopy.RemoveAt(i - 1);
                    listcopy.RemoveAt(i - 1);
                    i = i - 2;
                }
            }
        }
    }
}

where:

  • exclude – list that contains Duplicates only. This list might contain up to 30 elements.
  • listcopy – list that should be checked for duplicates. If duplicate from “exclude” is found -> perform removing operation. This list might contain up to 2000 elements.

I think that the LINQ might be some help but I don’t understand its syntax well.

  • 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-03T22:02:04+00:00Added an answer on June 3, 2026 at 10:02 pm

    A faster way (O(n)) would be to do the following:

    1. go through the exclude list and make it into a HashSet (O(n))
    2. in the checks, check if the element being tested is in the set (again O(n)), since test for presence in a HashSet is O(1).

    Maybe you can even change your algorithms so that the exclude collection will be a HashSet from the very beginning, this way you can omit step 1 and gain even more speed.

    (Your current way is O(n^2).)


    Edit:
    Another idea is the following: you are perhaps creating a copy of some list and make this method modify it? (Guess based on the parameter name.) Then, you can change it to the following: you pass the original array to the method, and make the method allocate new array and return it (your method signature should be than something like private List<int> getWithoutDoubles(HashSet<int> exclude, List<int> original)).


    Edit:
    It could be even faster if you would reorganize the input data in the following way: As the items are always removed in pairs (even index + the following odd index), you should pair them in advance! So that your list if ints becomes list of pairs of ints. This way your method might be be something like that:

    private List<Tuple<int, int>> getWithoutDoubles(
                  HashSet<int> exclude, List<Tuple<int, int>> original)
    {
        return original.Where(xy => (!exclude.Contains(xy.Item1) &&
                                     !exclude.Contains(xy.Item2)))
               .ToList();
    }
    

    (you remove the pairs where either the first or the second item is in the exclude collection). Instead of Tuple, perhaps you can pack the items into your custom type.

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

Sidebar

Related Questions

This is a question about Application design rather than how-to-do specific actions. I am
I have a question about precompiling ASP.NET web application projects from TeamCity. This is
I don't have so much a question about how to program something, but rather
I'm rather new to OOP and have a question about how to best deal
Have a rather simple question. Does anyone knows if i can use jparallax both
I have a question regarding a rather advanced DataModel which I would like to
I am aware that questions about recurring events are common but I have not
I have a question about optimization, but more on the browser/client side. I am
This is a design question rather than a Java-specific question, but I'm designing it
I've got a speed question. I have a bash script which parses information from

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.