Advertisement
NelIfandieva

MaxSequenceEqualElementsInList_ListsExer_Pr01

Feb 24th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text.RegularExpressions;
  6. namespace Learning
  7. {
  8.    
  9.     /* Read a list of integers and find the longest sequence of equal elements. If several exist, print the leftmost.*/
  10.  
  11.     class ListsExer_Problem01
  12.     {
  13.         static void Main()
  14.         {
  15.             char[] separators = {' '};
  16.             var input = Console.ReadLine()
  17.                                .Split(separators, StringSplitOptions.RemoveEmptyEntries)
  18.                                .Select(int.Parse)
  19.                                .ToList();
  20.             List<int> inputNums = new List<int>();
  21.             for (int index = 0; index < input.Count(); index++)
  22.             {
  23.                 inputNums.Add(input[index]);
  24.             }
  25.             int counter = 1;
  26.             int maxCounter = 0;
  27.             int longestSequence = 0;
  28.             for (int i = 0; i < inputNums.Count - 1; i++)
  29.             {
  30.                 if(inputNums[i] == inputNums[i + 1])
  31.                 {
  32.                     counter++;
  33.                     if(counter > maxCounter)
  34.                     {
  35.                         maxCounter = counter;
  36.                         longestSequence = inputNums[i];
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     counter = 1;
  42.                 }
  43.             }
  44.             if(maxCounter < counter)
  45.             {
  46.                 Console.Write(inputNums[0]);
  47.             }
  48.             else
  49.             {
  50.                 for (int i = 0; i < maxCounter; i++)
  51.                 {
  52.                     Console.Write("{0} ", longestSequence);
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement