Advertisement
stanevplamen

02.01.09.MaxEqualSequence

Jul 2nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxEqualSequence
  4. {
  5.     static void Main()
  6.     {      
  7.         int[] intArray = { 2, 1, 1, 2, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1 };
  8.         int counter = 1;
  9.         int maxRepeatCounter = 1;
  10.         int? maxRepeatNumber = null;
  11.         Array.Sort(intArray);
  12.         for (int i = 0; i < intArray.Length - 1; i++)
  13.         {
  14.             if (intArray[i] == intArray[i + 1])
  15.             {
  16.                 counter++;
  17.             }
  18.             else if (intArray[i] != intArray[i + 1])
  19.             {
  20.                 if (maxRepeatCounter < counter)
  21.                 {
  22.                     maxRepeatCounter = counter;
  23.                     maxRepeatNumber = intArray[i - 1];
  24.                 }
  25.                 counter = 1;
  26.             }
  27.         }
  28.         if (counter > maxRepeatCounter)
  29.         {
  30.             maxRepeatCounter = counter;
  31.             maxRepeatNumber = intArray[intArray.Length - 1];
  32.         }
  33.         Console.WriteLine(new string('-', 30));
  34.         Console.WriteLine("The most frequent number is: {0} ", maxRepeatNumber);
  35.         Console.WriteLine("It is repeating: {0} times", maxRepeatCounter);
  36.         Console.WriteLine(new string('-', 30));
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement