Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- Write a program that finds the maximal sequence of equal elements in an array.
- Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} => {2, 2, 2}.
- */
- namespace FindMaxSequenceOfEqualElements
- {
- public class MaxSequenceOfEqualElements
- {
- static void Main(string[] args)
- {
- //First Case: char array.
- #region
- char[] charArray = new char[6];
- Console.WriteLine("First Case: char array.");
- //Note: although "InitiliazeArray(T[] arr)" returns void, the array
- //passed as parameter is of reference type and is modified.
- // See http://pastebin.com/3TfRkFzF
- InitiliazeArray(charArray);
- Console.WriteLine("Initial array:");
- PrintArray(charArray);
- char[] biggestCharSubsequence = BiggestSequenceOfEqualValues(charArray);
- PrintArray(biggestCharSubsequence);
- #endregion
- //Second Case: int array.
- #region
- int[] intArray = new int[6];
- Console.WriteLine("Second Case: int array.");
- InitiliazeArray(intArray);
- Console.WriteLine("Initial array:");
- PrintArray(intArray);
- int[] biggestIntSubsequence = BiggestSequenceOfEqualValues(intArray);
- PrintArray(biggestIntSubsequence);
- #endregion
- }
- public static void InitiliazeArray<T>(T[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write("Enter value {0}: ", i + 1);
- string currentArrValue = Console.ReadLine();
- //See :http://stackoverflow.com/questions/8625/generic-type-conversion-from-string
- //See: http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html
- arr[i] = (T)Convert.ChangeType(currentArrValue, typeof(T));
- }
- }
- //The array of equal elements that is searched.
- static T[] CreateEqualElementsArray<T>( int length, T value)
- {
- T[] arr = new T[length];
- for (int i = 0; i < length; i++)
- {
- arr[i] = value;
- }
- return arr;
- }
- public static void PrintArray<T>(T[] arr)
- {
- Console.WriteLine();
- foreach (T item in arr)
- {
- Console.Write(item + " ");
- }
- Console.WriteLine("\n//-----------------------------------//");
- }
- static T[] BiggestSequenceOfEqualValues<T>(T[] arr)
- {
- int currentMaxLength = 1;
- int currentLength = 1;
- int currentIndex = 0;
- T currentBiggestSybsequenceValue = arr[0];
- //Find biggest subsequence length and value.
- while (currentIndex<arr.Length -1)
- {
- //while index is inside array and next element is equal to current element
- while (currentIndex<arr.Length -1 &&
- arr[currentIndex].Equals(arr[currentIndex + 1]))
- {
- currentLength++;
- currentIndex++;
- }
- if (currentLength > currentMaxLength)
- {
- currentMaxLength = currentLength;
- currentBiggestSybsequenceValue = arr[currentIndex];
- }
- //At this point arr[currentIndex + 1] differs -
- //it gives a start to new subsequence. But we are still at the last
- //element of the current sequence, so wi must increase index by 1.
- currentIndex++;
- currentLength = 1;
- }
- T[] biggestSubsequence =
- CreateEqualElementsArray(currentMaxLength, currentBiggestSybsequenceValue);
- return biggestSubsequence;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement