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
With (OP) 10 random doubles quite fast. The chance of a repeat: ~0.000002 %.
static bool repeat(double[] a)
{
return
a[0] == a[1] || a[0] == a[2] || a[0] == a[3] || a[0] == a[4] ||
a[0] == a[5] || a[0] == a[6] || a[0] == a[7] || a[0] == a[8] ||
a[0] == a[9] || a[1] == a[2] || a[1] == a[3] || a[1] == a[4] ||
a[1] == a[5] || a[1] == a[6] || a[1] == a[7] || a[1] == a[8] ||
a[1] == a[9] || a[2] == a[3] || a[2] == a[4] || a[2] == a[5] ||
a[2] == a[6] || a[2] == a[7] || a[2] == a[8] || a[2] == a[9] ||
a[3] == a[4] || a[3] == a[5] || a[3] == a[6] || a[3] == a[7] ||
a[3] == a[8] || a[3] == a[9] || a[4] == a[5] || a[4] == a[6] ||
a[4] == a[7] || a[4] == a[8] || a[4] == a[9] || a[5] == a[6] ||
a[5] == a[7] || a[5] == a[8] || a[5] == a[9] || a[6] == a[7] ||
a[6] == a[8] || a[6] == a[9] || a[7] == a[8] || a[7] == a[9] ||
a[8] == a[9];
}
More general, with 10 numbers ~2 times slower than above,
but ~7 times faster than the hashset approach.
static bool repeat(double[] a)
{
int k = a.Length - 1;
if (k < 70)
{
double aj;
for (int i = 0, j; i < k; )
{
for (aj = a[k--], j = k; j >= i; j--)
if (aj == a[j]) return true;
for (aj = a[i++], j = i; j <= k; j++)
if (aj == a[j]) return true;
}
return false;
}
var h = new HashSet();
while (k >= 0) if (!h.Add(a[k--])) return false;
return true;
}
Two lines (slow with a repeat ;)
static bool repeat(double[] a)
{ return (new HashSet(a).Count < a.Length); }