You can do this with a little Linq:
if (testArray.Length != testArray.Distinct().Count())
{
Console.WriteLine("Contains duplicates");
}
Distinctthe extension method removes any duplicates, and Countgets the size of the result set. If they differ at all, then there are several duplicates in the list.
Alternatively, here is a more complex query, but it may be slightly more efficient:
if (testArray.GroupBy(x => x).Any(g => g.Count() > 1))
{
Console.WriteLine("Contains duplicates");
}
GroupBy , Any return true, - .
HashSet<T>, :
if (!testArray.All(new HashSet<double>().Add))
{
Console.WriteLine("Contains duplicates");
}
, , Linq:
var hashSet = new HashSet<double>();
foreach(var x in testArray)
{
if (!hashSet.Add(x))
{
Console.WriteLine("Contains duplicates");
break;
}
}