I would expect Enumerable.Distinct
to be about as fast as using a dictionary if you re only doing it once. If you want to be able to add/remove values and keep the distinct-ness, you could build a HashSet<string>
(which is basically what I expect Distinct is doing under the hood, but Distinct()
will obviously return new values as it finds them, maintaining order.
In fact, just using:
HashSet<string> distinctItems = new HashSet<string>(list);
will be a pretty good (and simple) solution if you don t mind the ordering being messed up. It s simpler than using a Dictionary
, and conceptually cleaner as well (as you don t really want to map keys to values).
(As ever, I would suggest finding the most readable solution first, and benchmark it - if it s "fast enough" then go with that. If you want to use this as part of another query, then Distinct
may well be the most readable way. Otherwise, I d suggest HashSet
.)