title: C# count consecutive duplicates in List<string>
body: <p>I have a List of strings and want to count the duplicates in it, to work with this information later. To simply count the duplicates would be very easy, but unfortunately I just want to count the consecutive duplicates.</p>
  
  <p>Let us say we have a list with this string items in it:</p>
  
  <blockquote>
    <p>"1A","3B","5X","7Q","2W","2G","2J","1A","2A"</p>
  </blockquote>
  
  <p>Now I want to count the duplicates in this list. <strong>I just will look at the first char of each string, the other characters in the string can be ignored!</strong>
  What we get is <em>2x "1%</em>" and <em>3x "2%"</em>, what i actually want to get is consecutive duplicates, so my result should look like <em>3x "2%"</em>. The <em>2x "1A"</em> has to be ignored, they are not in a row.
  (% = place holder)</p>
  
  <p>I wrote a code that loops through the list and compares one string with the next one</p>
  
  <pre><code>int counter = 0;
  for (int i = 0; i &lt; list.Count; i++)
  {
      char first = list[i][0];
  
      if ((i + 1) == list.Count) break;
      char second = list[(i + 1)][0];
  
      if (first == second)
      {
          counter++;
      }
  }
  </code></pre>
  
  <p>I guess you can imagine that this code is a very ugly way to do this especially if you want to work with the output. Also my code can´t handle the features I need. </p>
  
  <p>The code I am looking for, must be able to deal with two feature I want to implement. First, a row of duplicates does not end if the last element of my list is equal to the first element of the list.</p>
  
  <p>For example:</p>
  
  <blockquote>
    <p>"1A","1B","5X","7Q","2J","1I"</p>
  </blockquote>
  
  <p>The "1%" has to be detected as duplicate, because of the "1I" and "1A" which are "in a row". If you would loop through the list you just break up at the end of the list if the first and last element are not equal.</p>
  
  <p>pseudo code:</p>
  
  <pre><code>if(list.First()[0] != list.Last()[0])
  </code></pre>
  
  <p>The second feature I want to implement is, that the items in the list that are not duplicates, with a "duplicate count" over 4 will be deleted. If there is not a single duplicate row with a "duplicate count" or length over 4 I want to return.</p>
  
  <p>For example:</p>
  
  <blockquote>
    <p>"1A","1B","5X","3Q","1J","1I"</p>
  </blockquote>
  
  <p>duplicate count == 4 so return</p>
  
  <blockquote>
    <p>"1A","1B","1X","3Q","1J","1I"</p>
  </blockquote>
  
  <p>duplicate <code>count == 5</code>, save this five items, delete any other item in the list.</p>
  
  <blockquote>
    <p>"1A","1B","1X","3Q","1I","1Z","1Z"</p>
  </blockquote>
  
  <p>duplicate count == 6, save this six items, delete any other item in the list.</p>
  
  <p><strong>Notice:
  Just the first char of each string matters. The input list will have 7 items, not a single item more or less. There is no result list, the old one has to be updated. If the duplicate count is under or equal to 4, then there is no work to do, simply return.
  There will not be more than 5 duplicates in a row. I have to check billion of list, so performance is really important</strong></p>
  
  <p>As they don´t teach any better English in German schools, I hope anyone understand what my problem is and is willing to help me out.</p>
  
  <p>This is not part of any homework.</p>