Advertisement
DimiPetrov

Longest Sequence of Equal

Jan 8th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. let input = ['10', ['2', '1', '1', '2', '3', '3', '2', '2', '2', '1']];
  2. let print = this.print || console.log;
  3. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  4.  
  5. const n = +gets();
  6. const arr = gets();
  7.  
  8. let startingIndex = 0;
  9. let count;
  10. let finalIndex = 0;
  11. let maxCount = 0;
  12.  
  13. for(let i = 0; i < n; i++) {
  14.  
  15. startingIndex = i;
  16. count = 0;
  17. let elToCompare = Number(arr[i]);
  18. for(let j = i; j < n; j++) {
  19. let el = Number(arr[j]);
  20. if(el === elToCompare) {
  21. count++;
  22. } else {
  23. break;
  24. }
  25. }
  26. if(count > maxCount) {
  27. maxCount = count;
  28. finalIndex = startingIndex;
  29. }
  30. }
  31.  
  32. let result = arr.splice(finalIndex, maxCount);
  33. print(result.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement