Arrays_maxSequenceOfEqualElements

Oct 6th, 2021 (edited)
1,122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function solve(array) {
2.     let arr = array
3.     let arrL = array.length
4.     let repeatCount = 1
5.     let repeatNum
6.     let repeatBest = 1
7.     let repeatNumTop = 0
8.
9.     for (let i = 0; i < arrL; i++) {
10.         let currentNum = arr[i]
11.         let nextNum = arr[i + 1]
12.
13.         if (currentNum !== nextNum) {
14.             repeatCount = 1
15.         }
16.
17.         if (currentNum === nextNum) {
18.             repeatCount++
19.             repeatNum = currentNum
20.
21.             if (repeatCount > repeatBest) {
22.                 repeatBest = repeatCount
23.                 repeatNumTop = repeatNum
24.
25.             }
26.         }
27.     }
28.     repeatNumTop+=" "
29.     let result = repeatNumTop.repeat(repeatBest)
30.
31.     console.log(result)
32. }
33.
34. solve([2, 1, 1, 2, 3, 3, 2, 2, 2, 3, 3, 1])
RAW Paste Data