Searching the same values in an array
You can use a Dictionary&lt;int, int&gt; of positions of the first occurrence of each element value.
Next, iterating over all elements of the input array, you do:


If the positions[value] is empty, store the current index i in it: positions[value] = i.
Else, calculate the distance (or cycle length).
If this distance is greater than maximum, save it as a new maximum.


This algorithm has time complexity \$O(n)\$.

The code:

public static class CycleSearch
{
    public static int GetMaximumCycle(int[] array)
    {
        Dictionary&lt;int, int&gt; positions = new Dictionary&lt;int, int&gt;();

        int result = 0;
        for (int i = 0; i &lt; array.Length; i++)
        {
            int value = array[i];
            int position;
            if (!positions.TryGetValue(value, out position))
            {
                positions[value] = i;
            }
            else
            {
                int cycleLength = i - position;
                if (cycleLength &gt; result)
                {
                    result = cycleLength;
                }
            }
        }
        return result;
    }
}


Usage:

int[] exampleArray = { 4, 6, 2, 2, 6, 6, 1, 55, 55, 2, 5, 6, 90, 8, 8, 8, 10, 70 };
Console.WriteLine(CycleSearch.GetMaximumCycle(exampleArray));


Output:


  10