Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 7. Max Sequence of Equal Elements
- Write a program that finds the longest sequence of equal elements in an array of integers. If several longest sequences exist, print the leftmost one.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _07MaxSequenceOfEqualElements
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] array = Console.ReadLine().Split();
- int bestCount = 0;
- string bestIndex = "";
- for (int i = 0; i < array.Length; i++)
- {
- var currentElement = array[i];
- int currentCount = 1;
- for (int a = i + 1; a < array.Length; a++)
- {
- if (currentElement == array[a])
- {
- currentCount++;
- }
- else
- {
- break;
- }
- }
- if (currentCount > bestCount)
- {
- bestCount = currentCount;
- bestIndex = currentElement;
- }
- }
- string result = "";
- for (int i = 0; i < bestCount; i++)
- {
- result += bestIndex + " ";
- }
- Console.WriteLine(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment