Program to find a list of numbers where each K-sized window has unique elements in Python

Given a list of numbers and a window size k, we need to find the count of distinct numbers in each sliding window of size k. This is a common sliding window problem that can be efficiently solved using a dictionary to track element frequencies.

So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4].

Algorithm

To solve this, we will follow these steps −

  • Initialize a counter dictionary to track element frequencies in the current window

  • Fill the first window of size k

  • For each subsequent position, slide the window by:

    • Adding the count of distinct elements to result

    • Adding the new element entering the window

    • Removing the element leaving the window

    • Deleting elements with zero frequency

  • Add the final window's distinct count

Implementation

from collections import Counter

def count_distinct_in_windows(nums, k):
    # Initialize counter for first window
    counter = Counter()
    for i in range(k):
        counter[nums[i]] += 1
    
    result = []
    
    # Slide the window
    for i in range(k, len(nums)):
        # Add count of distinct elements in current window
        result.append(len(counter))
        
        # Add new element entering the window
        counter[nums[i]] += 1
        
        # Remove element leaving the window
        counter[nums[i - k]] -= 1
        
        # Remove element if frequency becomes 0
        if counter[nums[i - k]] == 0:
            del counter[nums[i - k]]
    
    # Add count for the last window
    result.append(len(counter))
    
    return result

# Test the function
nums = [2, 2, 3, 3, 4]
k = 2
print(count_distinct_in_windows(nums, k))
[1, 2, 1, 2]

How It Works

Let's trace through the example with nums = [2, 2, 3, 3, 4] and k = 2:

  • Initial window [2, 2]: counter = {2: 2}, distinct count = 1

  • Window [2, 3]: counter = {2: 1, 3: 1}, distinct count = 2

  • Window [3, 3]: counter = {3: 2}, distinct count = 1

  • Window [3, 4]: counter = {3: 1, 4: 1}, distinct count = 2

Alternative Implementation Using Class

from collections import Counter

class Solution:
    def solve(self, nums, k):
        counter = Counter()
        for i in range(k):
            counter[nums[i]] += 1
        
        result = []
        for i in range(k, len(nums)):
            result.append(len(counter))
            counter[nums[i]] += 1
            counter[nums[i - k]] -= 1
            if counter[nums[i - k]] == 0:
                del counter[nums[i - k]]
        
        result.append(len(counter))
        return result

# Test the solution
solution = Solution()
nums = [2, 2, 3, 3, 4]
print(solution.solve(nums, 2))
[1, 2, 1, 2]

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the input array

  • Space Complexity: O(k), for storing at most k distinct elements in the counter

Conclusion

This sliding window approach efficiently counts distinct elements in each k-sized window using a frequency counter. The key insight is maintaining the window by adding new elements and removing old ones while tracking distinct counts.

Updated on: 2026-03-25T11:12:57+05:30

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements