Find the peak second with a sliding window
function peakRate(times, windowMs = 1000) { let peak = 0; let start = 0; for (let end = 0; end < times.length; end += 1) { while (times[end] - times[start] >= windowMs) start += 1; peak = Math.max(peak, end - start + 1); } return peak;}
// Press times in milliseconds since the first press.const times = [0, 900, 1800, 2700, 2800, 2900, 3000, 3100, 4500];console.log(peakRate(times)); // 5The window ends at each press and reaches back one second. Presses that have fallen out of it are dropped from the start, and the largest window seen is the peak. Here the five presses between 2700 and 3100 milliseconds share one second.