Advertisement
dimov

Untitled

Jan 6th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. /* Write a program that finds the maximal sequence of equal elements in an array.
  2.         Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}. */
  3.  
  4. using System;
  5.  
  6. class MaxSequence
  7. {
  8.     static void Main()
  9.     {
  10.         int[] array1 = new int[10] { 2, 1, 1, 2, 3, 3, 3, 2, 2, 1 };
  11.         int counter = 0;
  12.         int bigCounter = 0;
  13.         int which1 = 0;
  14.  
  15.         for (int i = 0; i < array1.Length-1; i++)
  16.         {
  17.             if (array1[i] == array1[i + 1])
  18.             {
  19.                 counter++;
  20.                 if (bigCounter <= counter)
  21.                 {
  22.                     bigCounter = counter;
  23.                     which1 = array1[i];
  24.                 }
  25.             }
  26.             else
  27.             {
  28.                 counter = 0;
  29.             }
  30.         }
  31.  
  32.         Console.WriteLine("Longest streak is made of {0} consecutive {1}'s",bigCounter+1, which1);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement