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);
    }
}

bestArray is indeed a System.int32[]. So this is printed out by Console.WriteLine.
Use a for-loop and the Length property of bestArray to print out all the integers in the array.

I am getting no output, I have to interrupt the command prompt.

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();
        for (int i = 0; i < bestArray.Length; i++)
                {
                    Console.Write(',' + bestArray[i]);
                }
    }
}

Edited 2 Years Ago by flebber

it doesn't seem to matter what I change about it I don't get a result.

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();

                for (int b = 0; b < bestArray.Length; b++)
                {
                    Console.Write(',' + bestArray[b]);
                }
            }

        }


    }
}

Now bestArray has a Length of 0, so nothing is printed.
Will try to figure that out. Nice little problem for my weekend!

I have solved it.

using System;
using System.Linq;

class Arr4
{
    static void Main()
    {
        int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1, 5, }; // Output wanted {2,2,2};
        int bestCount = 0;
        int start = 0;
        int a = 0;

        for(int i = 0; i < testArray.Length -1; i++)
        {
            int count = 0;
            while (testArray[i] == testArray[a])
            {
                count += 1;
                a += 1;
                if (count > bestCount)
                {
                    start = i;
                    bestCount = count;
                }
            }
        }
        int end = bestCount + start;
        Console.WriteLine("The start is {0} and count is {1}", start, bestCount);
        var bestArray = testArray.Skip(start)
                         .Take(end - start)
                         .ToArray();
        Console.Write("{ ");
        Console.Write(string.Join(", ", bestArray));
        Console.Write(" }");
    }   

}

output is

C:\Users\Sayth\Documents\Scripts>arr4
The start is 4 and count is 7
{ 2, 2, 2 }
C:\Users\Sayth\Documents\Scripts>

Edited 2 Years Ago by flebber

FYI: this is what I came up with:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};

            int index = 0;
            Dictionary<int, List<int>> bin = new Dictionary<int, List<int>>();
            List<int> maxBin = new List<int>();
            maxBin.Add(testArray[0]);
            for (int i = 1; i < testArray.Length; i++)
            {
                if (testArray[i - 1] == testArray[i])
                {
                    maxBin.Add(testArray[i]);
                }
                else
                {
                    bin.Add(index, maxBin);
                    index++;
                    maxBin = new List<int>();
                    maxBin.Add(testArray[i]);
                }
            }
            bin.Add(index, maxBin);

            Dictionary<int, List<int>>.ValueCollection subLists = bin.Values;
            var res = subLists.OrderByDescending(x => x.Count).First().ToList();

            StringBuilder sb = new StringBuilder();
            sb.Append("{ ");
            foreach (int item in res)
            {
                sb.Append(item);
                sb.Append(", ");
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append(" }");
            Console.WriteLine("The required output is: {0}", sb.ToString());

            Console.ReadKey();
        }
    }               
}
This question has already been answered. Start a new discussion instead.