lmarkov

Maximal Sequence Of Equal Elements In Array

Jan 24th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. /*
  2.  Write a program that finds the maximal sequence of equal elements in an array.
  3.         Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}.
  4.  */
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace MaximalSequenceOfEqualElementsInArray
  13. {
  14.     class MaximalSequenceOfEqualElementsInArray
  15.     {
  16.         static void Main()
  17.         {
  18.             int[] myArray = {4,5,6,6,6,7,7,8,8,8,8,8,4,4,4,4,4,3,4,4,4,4,4,4 };
  19.             int counter=1, maxCount=0;
  20.             int elementValue=0;
  21.             for (int i = 1; i < myArray.Length; i++)
  22.             {
  23.                 if (myArray[i-1] == myArray[i])
  24.                 {
  25.                     counter++;
  26.                     if (counter > maxCount)
  27.                     {
  28.                         maxCount = counter;
  29.                         elementValue = myArray[i];
  30.                     }                    
  31.                 }
  32.                 else
  33.                 {
  34.                     counter = 1;
  35.                 }
  36.             }
  37.  
  38.             Console.WriteLine("\"{0}\" -> {1} times is the first longest sequence", elementValue, counter);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment