I have a lot of code in which I do something like this
bool GetIsUnique(IEnumerable<T> values)
{
return values.Count() == values.Distinct().Count;
}
Is there a better faster nicer way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your method needs to iterate through the sequence twice, with a few of potential drawbacks:
Countwhich needs to iterate the entire sequence each time. There’s no reason why you shouldn’t break-out early as soon as you know that there’s a duplicate value.The following method only needs to iterate through the sequence once, and will break-out early as soon as any duplicate value is encountered: