what is fastest way to remove duplicate values from a list.
Assume List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in use lambda to remove duplicate and returned : {1, 2, 3, 4, 5}. What is your suggestion?
what is fastest way to remove duplicate values from a list. Assume List<long> longs
Share
The easiest way to get a new list would be:
Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded.
Note that
Distinct()isn’t guaranteed to preserve the original order, but in the current implementation it will – and that’s the most natural implementation. See my Edulinq blog post aboutDistinct()for more information.If you don’t need it to be a
List<long>, you could just keep it as:At this point it will go through the de-duping each time you iterate over
uniquethough. Whether that’s good or not will depend on your requirements.