Getting unique items from a list

domgreen picture domgreen · Sep 7, 2009 · Viewed 183.8k times · Source

What is the fastest / most efficient way of getting all the distinct items from a list?

I have a List<string> that possibly has multiple repeating items in it and only want the unique values within the list.

Answer

LukeH picture LukeH · Sep 7, 2009

You can use the Distinct method to return an IEnumerable<T> of distinct items:

var uniqueItems = yourList.Distinct();

And if you need the sequence of unique items returned as a List<T>, you can add a call to ToList:

var uniqueItemsList = yourList.Distinct().ToList();