The problem. Given an array arr and a number k, return how many distinct numbers each window of k consecutive elements contains — one count per window, from left to right. An array of n elements has n − k + 1 windows.
arr = [1, 2, 1, 3, 4, 2, 3], k = 4 -> [3, 4, 4, 3]
arr = [4, 1, 1], k = 2 -> [2, 1]
arr = [5, 5, 5, 5], k = 3 -> [1, 1]Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson