Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //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 _04.SequenceOfEqualElements
- {
- class SequenceOfEqualElements
- {
- static void Main(string[] args)
- {
- //note it will find the first Max Sequence for example
- //{ 2, 1, 1, 2, 3, 3, 2, 4, 5, 6, 1 } the max sequence is {1,1}, not {3,3}
- int[] myArray = { 1, 1, 3, 4, 5, 6, 7, 8, 9, 7, 7 };
- int counter=1; //initiliaze counter for the sequence
- int maxSeq = 0; //max sequence
- int bestNumber=0; //number with the max sequence
- //check till the pre-last element because we compare i == i+1 checks the next element so it will be the last
- for (int i = 0; i < myArray.Length-1; i++)
- {
- //counter = 1; //the present number by itself is sequence of 1
- if (myArray[i] == myArray[i+1])
- {
- counter++;
- }
- else
- {
- if (counter > maxSeq)
- {
- maxSeq = counter;
- bestNumber = myArray[i];
- counter = 1;
- }
- }
- }
- //maybe we have to put it in an array but this is shorter :)
- for (int i = 0; i < maxSeq; i++)
- {
- Console.Write("{0} ", bestNumber);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement