-1

(Tried to look for earlier answers for my question and didn't find any...)

lets say I have an array like this :

string[] chars = {"1", "x", "1", "x", "x", "x", "x", "x", "1", "1", "1", "x", "1", "x", "x", "x"};

I need to find the way to extract the max number of "x" in a row in an array, so in this example I have total of 10 "x" but only 5 in a row, so I need the extract the number 5.

Tried this method... but how of course it wont work with the first char (i-1).

  string[] chars = { "1", "x", "1", "x", "x", "x", "x", "x", "1", "1", "1", "x", "1", "x", "x", "x" };
        int count = 0;
        for (int i=0; i < chars.Length; i++)
        {

            if ((chars[i] == "x") && (chars[i] == chars[i - 1])) ;
     
                count++;
        }
        Console.WriteLine(count);

Thanks for the help !

Yuvi1
  • 9
  • 7
  • Use LINQ's `GroupBy`, `OrderByDescending`, `Count` for the grouped items, and `First` (or `FirstOrDefault` if the list can be empty). – ProgrammingLlama Jan 06 '21 at 06:51
  • Yes i tried with for loop that count chars of "x" and stops if next char is not the same as the last one, but it stops counting before the sequence. – Yuvi1 Jan 06 '21 at 06:52
  • 5
    Would you like to provide the code that wasn't working? This way we can give you more constructive feedback while not spoon feeding you the answer. – StealthBadger747 Jan 06 '21 at 06:53
  • @KunalMukherjee _["Not all questions benefit from including code..."](https://stackoverflow.com/help/how-to-ask)_ and _[Is it always a good idea to demand the OP “post some code”?](https://meta.stackoverflow.com/a/286760/585968)_ –  Jan 06 '21 at 07:05
  • 1
    _"best way"_ is _subject to opinion_ making the question arguably off-topic for SO –  Jan 06 '21 at 07:06
  • Just edited the question and added the code.. how do i solved this with the first char, because char[i-1] wont work in the first char – Yuvi1 Jan 06 '21 at 07:10
  • i deleted the "best way" part, I hope the question was not deleted, just want a solution for the if statement, thanks. – Yuvi1 Jan 06 '21 at 07:15

1 Answers1

2

Low tech generic approach with just a foreach and an iterator method

Given

public static IEnumerable<(T item, int count)> GetStuff<T>(IEnumerable<T> source)
{
   T current = default;
   var started = false;
   var count = 0;
   foreach (var item in source)
   {
      if (!EqualityComparer<T>.Default.Equals(item,current) && started)
      {
         yield return (current, count);
         count = 0;
      }
      current = item;
      count++;
      started = true;
   }
   yield return (current, count);
}

Usage

string[] chars = {"1", "x", "1", "x", "x", "x", "x", "x", "1", "1", "1", "x", "1", "x", "x", "x"};

var results = GetStuff(chars);

foreach (var result in results)
   Console.WriteLine(result);

Results

(1, 1)
(x, 1)
(1, 1)
(x, 5)
(1, 3)
(x, 1)
(1, 1)
(x, 3)

If you wanted the max of something

var results = GetStuff(chars)
    .Where(x => x.Item == "x")
    .Max(x => x.Count);
TheGeneral
  • 79,002
  • 9
  • 103
  • 141