Advertisement
Ludmil

C# array 4

Jun 29th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. //4.    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. // should cover all cases ...
  4. using System;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         int[] arr =
  11.             //{ 1, 7, 3, 3, 5, 5 }; /*ok if(max != 0 && indexValue[i]!=0)*/
  12.             { 1, 1, 3, 3, 5, 5 };// this check was hard
  13.         //{ 1,2,3,4,5,6 };/*ok if (max == 0 )*/
  14.         //{ 1, 1, 1,1,2, 2, 2, 2, 3, 3, 3,3 };/*ok*/
  15.         //{2, 1, 1, 2, 3, 3, 2, 2, 2, 1} ;
  16.  
  17.         int[] counterMax = new int[arr.Length];// how many times element met in array
  18.         int counterForArrCount = 0;// count repeated element
  19.         int[] indexValue = new int[arr.Length];// indexator for repeated elements in arr[]
  20.         for (int i = 0; i < arr.Length - 1; i++)// check all elements witout the last one
  21.         {
  22.             if (arr[i] == arr[i + 1])// here we check as well the last one
  23.             {
  24.                 counterForArrCount++;
  25.                 counterMax[i] = counterForArrCount;//how many time met this element
  26.             }
  27.             else
  28.             {
  29.                 counterForArrCount = 0; // back to zero
  30.                 counterMax[i] = counterForArrCount;
  31.             }     //no need separate check for last element -    
  32.         }
  33.         int max = 0;// find max repeated elm
  34.         for (int i = 0; i < counterMax.Length; i++)
  35.         {
  36.             if (counterMax[i] > max)
  37.             {
  38.                 max = counterMax[i];
  39.             }
  40.         }// indexing if max repeated lenght is met more then 1
  41.         for (int i = 0; i < counterMax.Length; i++) // smesto 0->1
  42.         {
  43.             if (counterMax[0] == max)//special check for case: { 1,1,3,3,5,5 };
  44.             {
  45.                 indexValue[1] = 1;
  46.             }
  47.             if (counterMax[i] == max)
  48.             {
  49.                 indexValue[i] = i;
  50.             }
  51.         }// print out        
  52.         for (int i = 0; i < indexValue.Length; i++)
  53.         {   // print just longest
  54.             if (indexValue[i] > 0 && max != 0)
  55.             {// get which walue to print prom original arr[]
  56.                 int getValue = indexValue[i];
  57.                 Console.Write("{");// just fancy
  58.                 for (int j = 0; j <= max; j++)
  59.                 {                    
  60.                     if (j < max)
  61.                     {
  62.                         Console.Write("{0}, ", arr[getValue]);
  63.                     }
  64.                     else if (j == max)
  65.                     {
  66.                         Console.Write("{0}", arr[getValue]);
  67.                     }
  68.                 }
  69.                 Console.WriteLine("}");// just fancy
  70.             }
  71.             if (max == 0) // special check for case { 1,2,3,4,5,6 }
  72.             {
  73.                 int getValue = indexValue[i];
  74.                 Console.Write("{");// just fancy
  75.                 for (int j = 0; j <= max; j++)
  76.                 {
  77.                     if (j < max)
  78.                     {
  79.                         Console.Write("{0}, ", arr[getValue]);
  80.                     }
  81.                     else if (j == max)
  82.                     {
  83.                         Console.WriteLine("{0}", arr[getValue]);
  84.                     }
  85.                 }
  86.                 Console.WriteLine("}");// just fancy
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement