JulianJulianov

07. Max Sequence of Equal Elements

Feb 6th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. 7. Max Sequence of Equal Elements
  2. 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.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _07MaxSequenceOfEqualElements
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. string[] array = Console.ReadLine().Split();
  17.  
  18. int bestCount = 0;
  19. string bestIndex = "";
  20. for (int i = 0; i < array.Length; i++)
  21. {
  22. var currentElement = array[i];
  23. int currentCount = 1;
  24. for (int a = i + 1; a < array.Length; a++)
  25. {
  26. if (currentElement == array[a])
  27. {
  28. currentCount++;
  29. }
  30. else
  31. {
  32. break;
  33. }
  34. }
  35. if (currentCount > bestCount)
  36. {
  37. bestCount = currentCount;
  38. bestIndex = currentElement;
  39. }
  40. }
  41. string result = "";
  42. for (int i = 0; i < bestCount; i++)
  43. {
  44. result += bestIndex + " ";
  45. }
  46. Console.WriteLine(result);
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment