Hello,

In a method i am calculating the longest row of elements. The 1-dim array is filled up with random values (0 or 1).
The method looks up the longest row (being 0 or 1, whatever is the longest).
Meaning in for example:

1110100 --> the longest row would be 3 (3 * 1)
0110000 --> the longest row would be 4 (4 * 0)

My problem is i am trying to perform some type of linear search to show the position of the row in the array (seperate method to keep it nice and tidy).
The first example has the longest row of 3 elements (3 times 1).

For 1110100 the position in the array would be 0 - 2 (index)
For 0110000 the position in the array would be 3 - 6 (index)

I have been trying with foreaches, for loops etc..but i cannot seem to get the proper indexes of both.
Cannot seem to display both positions properly. Been trying for hours.

For the first example the correct output wouldbe: The largest row of same elements of the array consists of 3 elements on the position 0 -> 2.

The longest row of elements gets of same elements get calculated as the following:

public int BerekenDeelrij (int [] table)
        (
            int count = 0;
            int LastValue = 0,
            int largest = 0;


            for (int i = 0; i <tabel.Length; i++)
            (
                if (LastValue ==  table[i])
                    counter++;
                else
                (
                    = Math.Max largest (largest, counter);
                    LastValue = table[i];
                    counter = 1;
                )
            )
            Math.Max return (largest, counter);
        )

Best Regards.

There may be a better method than this. In fact, I'm almost positive I've seen one, but here's an idea. This one uses .NET 4 and the new Tuple, but it can be easily adapted to use a concrete class or perhaps out parameters.

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

namespace DaniWebDemo
{
    class Program
    {
        static void Main()
        {
            int[] array1 = { 1, 1, 1, 0, 1, 0, 0 };
            int[] array2 = { 0, 1, 1, 0, 0, 0, 0 };

            Tuple<int, int, int> array1Longest = FindLongestSubsetOfEqualElements(array1);
            Tuple<int, int, int> array2Longest = FindLongestSubsetOfEqualElements(array2);
            Tuple<string, int, int> nullTest = FindLongestSubsetOfEqualElements(new string[] { string.Empty, null });

            Action<Tuple<int, int, int>> writer = tuple =>
                {
                    Console.WriteLine("The longest subset features element {0}, {1} times, starting at index {2}.",
                        tuple.Item1,
                        tuple.Item2,
                        tuple.Item3);
                };

            writer(array1Longest);
            writer(array2Longest);

            Console.Read();
        }

        static Tuple<T, int, int> FindLongestSubsetOfEqualElements<T>(IEnumerable<T> elements)
        {
            if (!elements.Any())
                throw new ArgumentException("elements");

            // values for current max
            int startingIndex = 0;
            int elementCount = 0;
            T maxElement = default(T);

            // temporary values for comparison
            int currentIndex = 0;
            int currentCount = 0;

            T lastElement = elements.First();

            foreach (var item in elements.Select((elem, index) => new { Element = elem, Index = index }))
            {
                if (item.Element == null && lastElement == null)
                {
                    currentCount++;
                }
                else if (item.Element != null && item.Element.Equals(lastElement))
                {
                    currentCount++;
                }
                else
                {
                    currentIndex = item.Index;
                    currentCount = 1;
                    lastElement = item.Element;
                }

                if (currentCount > elementCount)
                {
                    startingIndex = currentIndex;
                    elementCount = currentCount;
                    maxElement = lastElement;
                }
            }

            return new Tuple<T, int, int>(maxElement, elementCount, startingIndex);
        }
    }
}

Ok. Here's one possible solution. Hopefully this will help a little bit. What you want to do is pass in the int[], as well as an out variable to put in either your run count or the final index of the longest run.

static public int Function(int[] table, out int count)
{
    int counter = 0; // The current count of the run
    int finalCount = 1; // The highest run count
    int lastValue = 0; // The value of the previous array index - can also use [i-1]
    int finalIndex = 0; // The final index of the run
    for (int i = 0; i < table.Length; i++)
    {
        if (table[i] == lastValue) // If the current index matches the last value
        {
            counter++; // Current run increases
            if (finalCount < counter) // Final count should only be changed if less than the current run count
            {
                finalCount = counter;
                finalIndex = i + 1; // Current index is end of current highest run and accounts for base 0 indexing
            }
        }
        else
        {
            counter = 1; // Not a match, so the current run is now 1
        }

        lastValue = table[i]; // Set the lastValue to the current index
    }

    count = finalCount; // Set the out variable to the current count
    return finalIndex; // Return the last index of the highest run
}

With this method, you'll be able to take the final index, subtract the run length (out variable), and get the initial index.

I hope that all makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.