I am doing ex4 from the fundamentals of C#. But I cannot solve the problem. I am unable to slice the array correctly and instead just endup with System.int32[]
Simply all I am supposed to do is find the maxmimal sequence of equal consecutive elements. I am trying to follow the methodology they are using in the chapter.
using System;
using System.Collections.Generic;
using System.Linq;
class ArrayEx4
{
static void Main()
{
int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};
int count = 0;
int bestCount = 0;
int startArray = 0;
int endArray = 0;
int first = 0;
for (int i = 0; i < testArray.Length; i++)
{
if (testArray[i] == testArray[i+1])
{
count += 1;
i = first;
if (count > bestCount)
{
count = bestCount;
startArray = first;
endArray = first + bestCount;
}
}
}
int[] bestArray = testArray.Skip(startArray)
.Take(endArray - startArray)
.ToArray();
Console.WriteLine("The best array is {0}", bestArray);
}
}