C# Finding 2 positions 1-dimArray
Posted
by Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2010-05-25T08:34:34Z
Indexed on
2010/05/25
8:41 UTC
Read the original article
Hit count: 226
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.
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.
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;
final int value = 0;
int largest = 0;
foreach (int value in table)
(
if (value == last value)
counter + +;
else
(
largest = Math.Max largest (largest, counter);
final value = value
count = 1;
)
)
Math.Max return (largest, counter);
)
Best Regards.
© Stack Overflow or respective owner