Count Distinct Elements in Every Window

Given an integer array nums and an integer k, move every contiguous window of exactly k elements from left to right by one position at a time. Return an array…

Given an integer array `nums` and an integer `k`, move every contiguous window of exactly `k` elements from left to right by one position at a time. Return an array whose value at each position is the number of distinct elements in that window, in window order.

- `1 <= nums.length <= 10^5` - `-10^4 <= nums[i] <= 10^4` - `1 <= k <= nums.length` - The interactive visualizer accepts at most 12 values; longer valid inputs are execution-only.

Example 1: input nums = [1,2,1,3,4,2,3], k = 4; output [3,4,4,3]. Window by window: [1,2,1,3] has distinct values {1,2,3}, so 3; [2,1,3,4] has {1,2,3,4}, so 4; [1,3,4,2] has {1,2,3,4}, so 4; [3,4,2,3] has {2,3,4}, so 3. That gives [3,4,4,3].

Example 2: input nums = [1,1,1], k = 2; output [1,1]. Each window contains only the value 1.

Category: Sliding Window. Topic: sliding-window. Difficulty: Medium. Patterns: sliding-window, hashing.