Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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}.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MaximalSequenceOfEqualElementsInArray
- {
- class MaximalSequenceOfEqualElementsInArray
- {
- static void Main()
- {
- int[] myArray = {4,5,6,6,6,7,7,8,8,8,8,8,4,4,4,4,4,3,4,4,4,4,4,4 };
- int counter=1, maxCount=0;
- int elementValue=0;
- for (int i = 1; i < myArray.Length; i++)
- {
- if (myArray[i-1] == myArray[i])
- {
- counter++;
- if (counter > maxCount)
- {
- maxCount = counter;
- elementValue = myArray[i];
- }
- }
- else
- {
- counter = 1;
- }
- }
- Console.WriteLine("\"{0}\" -> {1} times is the first longest sequence", elementValue, counter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment