Advertisement
stefanpu

Longest sequence of equal elements

Jan 9th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.     Write a program that finds the maximal sequence of equal elements in an array.
  5.     Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} => {2, 2, 2}.
  6.  */
  7. namespace FindMaxSequenceOfEqualElements
  8. {
  9.     public class MaxSequenceOfEqualElements
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //First Case: char array.
  14.             #region
  15.             char[] charArray = new char[6];
  16.             Console.WriteLine("First Case: char array.");
  17.  
  18.             //Note: although "InitiliazeArray(T[] arr)" returns void, the array
  19.             //passed as parameter is of reference type and is modified.
  20.             // See http://pastebin.com/3TfRkFzF
  21.             InitiliazeArray(charArray);            
  22.             Console.WriteLine("Initial array:");
  23.             PrintArray(charArray);
  24.  
  25.             char[] biggestCharSubsequence = BiggestSequenceOfEqualValues(charArray);
  26.             PrintArray(biggestCharSubsequence);
  27.             #endregion
  28.  
  29.             //Second Case: int array.
  30.             #region
  31.             int[] intArray = new int[6];
  32.             Console.WriteLine("Second Case: int array.");
  33.             InitiliazeArray(intArray);
  34.            
  35.             Console.WriteLine("Initial array:");
  36.             PrintArray(intArray);
  37.  
  38.             int[] biggestIntSubsequence = BiggestSequenceOfEqualValues(intArray);
  39.             PrintArray(biggestIntSubsequence);
  40.             #endregion
  41.         }
  42.        
  43.         public static void InitiliazeArray<T>(T[] arr)
  44.         {
  45.             for (int i = 0; i < arr.Length; i++)
  46.             {
  47.                 Console.Write("Enter value {0}: ", i + 1);
  48.                 string currentArrValue = Console.ReadLine();
  49.  
  50.                 //See :http://stackoverflow.com/questions/8625/generic-type-conversion-from-string
  51.                 //See: http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html
  52.                 arr[i] = (T)Convert.ChangeType(currentArrValue, typeof(T));                
  53.             }            
  54.         }
  55.  
  56.         //The array of equal elements that is searched.
  57.         static T[] CreateEqualElementsArray<T>( int length, T value)
  58.         {
  59.             T[] arr = new T[length];
  60.             for (int i = 0; i < length; i++)
  61.             {
  62.                 arr[i] = value;              
  63.             }
  64.  
  65.             return arr;
  66.         }
  67.  
  68.         public static void PrintArray<T>(T[] arr)
  69.         {
  70.             Console.WriteLine();
  71.             foreach (T item in arr)
  72.             {
  73.                 Console.Write(item + " ");                
  74.             }
  75.             Console.WriteLine("\n//-----------------------------------//");            
  76.         }
  77.  
  78.         static T[] BiggestSequenceOfEqualValues<T>(T[] arr)
  79.         {
  80.             int currentMaxLength = 1;
  81.             int currentLength = 1;            
  82.             int currentIndex = 0;
  83.             T currentBiggestSybsequenceValue = arr[0];
  84.  
  85.             //Find biggest subsequence length and value.
  86.             while (currentIndex<arr.Length -1)
  87.             {
  88.                 //while index is inside array and next element is equal to current element
  89.                 while (currentIndex<arr.Length -1 &&
  90.                     arr[currentIndex].Equals(arr[currentIndex + 1]))
  91.                 {
  92.                     currentLength++;
  93.                     currentIndex++;
  94.                 }
  95.  
  96.                 if (currentLength > currentMaxLength)
  97.                 {
  98.                     currentMaxLength = currentLength;                    
  99.                     currentBiggestSybsequenceValue = arr[currentIndex];                    
  100.                 }
  101.                 //At this point arr[currentIndex + 1] differs -
  102.                 //it gives a start to new subsequence. But we are still at the last
  103.                 //element of the current sequence, so wi must increase index by 1.
  104.                 currentIndex++;
  105.                 currentLength = 1;
  106.             }
  107.  
  108.             T[] biggestSubsequence =
  109.                 CreateEqualElementsArray(currentMaxLength, currentBiggestSybsequenceValue);
  110.             return biggestSubsequence;
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement