andreykata

ArraysExercise04

Jan 12th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2.     class Program
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             int[] array = { 1, 1, 2, 2, 2, 3, 3, 2, 2, 1, 1, 1, 1 };
  7.             int valueSequence = 0;
  8.             int lengthSequence = 1;
  9.             int maxLengthSequence = 1;
  10.             for (int i = 0; i < array.Length-1; i++)
  11.             {
  12.                 if (array[i] == array[i+1])
  13.                 {                  
  14.                     lengthSequence += 1;
  15.                     if (lengthSequence > maxLengthSequence)
  16.                     {
  17.                         maxLengthSequence = lengthSequence;
  18.                         valueSequence = array[i];
  19.                     }
  20.                 }
  21.                 else
  22.                 {
  23.                     lengthSequence = 1;
  24.                 }
  25.                
  26.             }
  27.             Console.Write("The maximum sequence of equal elements in an array is: ");
  28.             for (int i = 0; i < maxLengthSequence; i++)
  29.             {
  30.                 Console.Write("{0} ",valueSequence);
  31.             }
  32.             Console.WriteLine();
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment