zornitza_gencheva

Task 4: the maximal sequence of equal elements

Jul 11th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FindMaxSequenceOfEqualElements
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //Write a program that finds the maximal sequence of equal elements in an array.
  14.  
  15. Console.WriteLine("Please enter numbers of elements of the array: ");
  16. int n = int.Parse(Console.ReadLine());
  17.  
  18. int[] array = new int[n];
  19.  
  20. int count = 1;
  21. int max = 0;
  22. int number = 0;
  23. int bigLenght = 0;
  24.  
  25. Console.WriteLine("Please enter elements of the array: ");
  26. for (int i = 0; i < n; i++)
  27. {
  28. array[i] = int.Parse(Console.ReadLine());
  29. }
  30.  
  31. for (int i = 0; i < n-1; i++)
  32. {
  33. if (array[i] == array[i + 1])
  34. {
  35. count += 1;
  36. max = array[i];
  37. }
  38. else
  39. {
  40. count = 1;
  41. }
  42. if (count > bigLenght)
  43. {
  44. number = max;
  45. bigLenght = count;
  46. }
  47. }
  48. Console.WriteLine();
  49.  
  50. for (int i = 0; i < bigLenght; i++)
  51. {
  52. if (bigLenght != 1)
  53. {
  54. Console.Write(number + " ");
  55. }
  56. else
  57. {
  58. Console.WriteLine("In this array, all the elements occurring once.");
  59. }
  60. }
  61. Console.Write(" --> {0} times", bigLenght);
  62. Console.WriteLine();
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment