Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- //Task4: 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 Task4MaxSequence
- {
- class MaxSequence
- {
- static void Main(string[] args)
- {
- int[] array = { 1, 2, 2, 2, 4, 4, 4, 4, 3, 1, 3, 4, 7, 1, 1, 1, 3, 2, 2, 1, 2, 2, 2 };
- int startindex = 0;
- int lengthequals = 1;
- int bestlength = 1;
- int beststart = 0;
- int length = array.Length;
- for (int i = 0; i < length - 1; i++)
- {
- if (array[i] == array[i + 1]) //check element by element
- {
- lengthequals++;
- if (lengthequals > bestlength)
- {
- bestlength = lengthequals;
- beststart = startindex;
- }
- }
- else
- {
- lengthequals = 1;
- startindex = i+1;
- }
- }
- for (int element = beststart; element < beststart + bestlength; element++)
- {
- Console.WriteLine(array[element]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement