I am looking for a way to quickly remove items from a C# List
. The documentation states that the List.Remove()
and List.RemoveAt()<
I've found when dealing with large lists, this is often faster. The speed of the Remove and finding the right item in the dictionary to remove, more than makes up for creating the dictionary. A couple things though, the original list has to have unique values, and I don't think the order is guaranteed once you are done.
List hundredThousandItemsInOrignalList;
List fiftyThousandItemsToRemove;
// populate lists...
Dictionary originalItems = hundredThousandItemsInOrignalList.ToDictionary(i => i);
foreach (long i in fiftyThousandItemsToRemove)
{
originalItems.Remove(i);
}
List newList = originalItems.Select(i => i.Key).ToList();