Fastest way to remove items from a List with thousands of items

Let’s say you have a list of 200,000 strings, and you need to remove 20,000 strings in a non-sequential order. You would probably loop through the list of items to remove each item from the original list:

            // Create a list with thousands of items
            List<string> originalList = new List<string>();
            for (int a = 0; a < 200000; a++)
            {
                // Add item to list
                originalList.Add("Item #" + a.ToString());
            }

            // Create a list of items to exclude
            List<string> excludedItems = new List<string>();
            for (int a = 0; a < 200000; a += 10)
            {
                // Add item to list
                excludedItems.Add("Item #" + a.ToString());
            }

            // Loop through original items and exclude items
            foreach (string excludedItem in excludedItems)
            {
                // Remove item
                originalList.Remove(excludedItem);
            }

This method works fine with a limited number of items, but becomes quite slow with bigger lists. It takes about 15 seconds to remove the 20,000 items from the list on a recent Intel Core i7 processor.

When dealing with lists of several thousands items, you should use List.Except instead:

            // Exclude list
            List<string> newList = originalList.Except(excludedItems).ToList();

The same operation now takes under 1 second to complete, thanks to LINQ!

 

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>