Advertisement
stambeto09

MaximalSequenceInArray

Dec 15th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 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 _04.MaximalSequenceInArray
  8. {
  9. class MaximalSequenceInArray
  10. {
  11. //Write a program that finds the maximal sequence of equal elements in an array.
  12.  
  13. static void Main()
  14. {
  15. Console.Write("Enter number of elements: ");
  16. int n = int.Parse(Console.ReadLine());
  17. int[] arr = new int[n];
  18.  
  19. for (int index = 0; index < arr.Length; index++)
  20. {
  21. arr[index] = int.Parse(Console.ReadLine());
  22. }
  23.  
  24. int[] thimes = new int[arr.Length];
  25. int[] numbers = new int[arr.Length];
  26.  
  27. for (int i = 0; i < arr.Length; i++)
  28. {
  29. numbers[i] = arr[i];
  30. thimes[i] = 1;
  31.  
  32. for (int l = i; l < arr.Length; l++)
  33. {
  34. //Checking if the current element is last;
  35. if (l == arr.Length-1)
  36. {
  37. break;
  38. }
  39. //Checking if next element is equal to next one;
  40. if (numbers[i] == arr[l+1])
  41. {
  42. thimes[i]++;
  43. }
  44. //If it's not-break;
  45. else
  46. {
  47. break;
  48. }
  49. }
  50. }
  51. //Getting the max value from thimes array.
  52. int maxTimes = thimes.Max();
  53. //Create a new variable that is equal to INDEX of maxTimes value;
  54. int position = Array.IndexOf(thimes, maxTimes);
  55.  
  56. Console.Write("{");
  57. //Write position times the element from numbers in position [position+i] :D ;
  58. for (int i = 0; i <thimes[position]; i++)
  59. {
  60. Console.Write(numbers[position+i]);
  61. }
  62. Console.Write("}");
  63.  
  64.  
  65.  
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement