I have a lot of code in which I do something like this
bool GetIsUnique(IEnumerable values)
{
return values.Count() == values.Distinct().Count;
}
>
You would be doing two loops through the data for the above - once to get the count, once to get the distinct count. Especially bad if the first two items are identical! Try something like this:
bool GetIsUnique(IEnumerable values)
{
HashSet hashSet = new HashSet();
foreach(var value in values)
{
if (hashSet.Contains(value))
{
return false;
}
hashSet.Add(value);
}
return true;
}
This one will finish as soon as it finds a duplicate. Obviously it on the speed of the hash lookup but given Distinct uses a set internally I'd still expect it to be quicker.