How do I check if my array has repeated values inside it?

前端 未结 8 1346
悲哀的现实
悲哀的现实 2020-12-01 20:55

So here is my array.

double[] testArray = new double[10];
// will generate a random numbers from 1-20, too lazy to write the code

I want to

8条回答
  •  醉梦人生
    2020-12-01 21:17

    Use this:

    bool CheckUniqueness(double[] values)
    {
        var uniqueValues = new HashSet();
        foreach (double d in values)
        {
            if(uniqueValues.Contains(d))
            {
                return false;
            }
            uniqueValues.Add(d);
        }
        return true;
    }
    

提交回复
热议问题