Advertisement
minusa71

Task4/Write a program that finds the maximal sequence of equ

Jan 12th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. //Task4: Write a program that finds the maximal sequence of equal elements in an array.
  6. //Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}.
  7.  
  8. namespace Task4MaxSequence
  9. {
  10. class MaxSequence
  11. {
  12. static void Main(string[] args)
  13. {
  14. int[] array = { 1, 2, 2, 2, 4, 4, 4, 4, 3, 1, 3, 4, 7, 1, 1, 1, 3, 2, 2, 1, 2, 2, 2 };
  15. int startindex = 0;
  16. int lengthequals = 1;
  17. int bestlength = 1;
  18. int beststart = 0;
  19. int length = array.Length;
  20. for (int i = 0; i < length - 1; i++)
  21. {
  22. if (array[i] == array[i + 1]) //check element by element
  23. {
  24.  
  25. lengthequals++;
  26.  
  27. if (lengthequals > bestlength)
  28. {
  29. bestlength = lengthequals;
  30. beststart = startindex;
  31.  
  32. }
  33.  
  34.  
  35. }
  36. else
  37. {
  38. lengthequals = 1;
  39. startindex = i+1;
  40. }
  41.  
  42. }
  43.  
  44. for (int element = beststart; element < beststart + bestlength; element++)
  45. {
  46. Console.WriteLine(array[element]);
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement