Sliding Window Maximum

Find the maximum value in each sliding window of size k as it moves across the array.

Code

Algorithms
const result = [];
for (let i = 0; i <= arr.length - k; i++) {
  result.push(Math.max(...arr.slice(i, i + k)));
}
return result;

Parameters

Array of numbers

Window size

Browser·fetch() may be limited by CORS

More JavaScript Snippets