问题
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.
回答1:
Use a HashSet<T>. For example:
var items = "A B A D A C".Split(' ');
var unique_items = new HashSet<string>(items);
foreach (string s in unique_items)
Console.WriteLine(s);
prints
A B D C
回答2:
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();
回答3:
You can use Distinct extension method from LINQ
回答4:
In .Net 2.0 I`m pretty sure about this solution:
public IEnumerable<T> Distinct<T>(IEnumerable<T> source)
{
List<T> uniques = new List<T>();
foreach (T item in source)
{
if (!uniques.Contains(item)) uniques.Add(item);
}
return uniques;
}
回答5:
Apart from the Distinct extension method of LINQ, you could use a HashSet<T> object that you initialise with your collection. This is most likely more efficient than the LINQ way, since it uses hash codes (GetHashCode) rather than an IEqualityComparer).
In fact, if it's appropiate for your situation, I would just use a HashSet for storing the items in the first place.
来源:https://stackoverflow.com/questions/1388361/getting-unique-items-from-a-list