If I am not mistaken, this method has a complexity \$O(N^2)\$:
int[] exampleArray = { 4, 6, 2, 2, 6, 6, 1, 55, 55, 2, 5, 6, 90, 8, 8, 8, 10, 70 };
int k = GetMaximumCycle(exampleArray);
public static int GetMaximumCycle(int[] anArray)
{
int length = anArray.Length;
int result = 0;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
if (anArray[i] == anArray[j])
{
if (!(result > Math.Abs(i - j)))
result = Math.Abs(i - j);
}
}
}
return result;
}
I compare values and gets the maximum difference between items of array.
The result variable is 10.
And if an input array is very large, this code works slowly. Is it possible to simplify the for loops or remove the second for loop to have a complexity of \$O(N)\$?