Best way to find out if IEnumerable<> has unique values

前端 未结 7 2138
轻奢々
轻奢々 2021-02-19 20:00

I have a lot of code in which I do something like this

bool GetIsUnique(IEnumerable values)
{
    return values.Count() == values.Distinct().Count;
}
         


        
7条回答
  •  醉话见心
    2021-02-19 20:49

    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.

提交回复
热议问题