Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find length of longest distinct sublist in Python
A distinct sublist is a continuous portion of a list in which all the elements are different from each other. The goal is to find the maximum length of such a sublist within a given list of elements.
In Python, a list is an ordered and mutable data structure in which sequence of elements are enclosed in square brackets []. The elements in a list can be of any datatype such as integer, float, string etc., and each element in a list has a unique index position which starts from 0.
Using Sliding Window with set()
The set() is a built-in function in Python which is used to create a set data structure to store unique elements. To find the longest sublist with all unique elements, we can use a sliding window approach. In this approach, we maintain a set to track elements in the current window. If a duplicate is found, we reduce the window size by moving the left pointer until the duplicate is removed.
Example
Following example shows how to use the sliding window technique with set() function to find the length of the longest distinct sublist ?
def longest_distinct_sublist(numbers):
seen = set()
left = 0
max_len = 0
for right in range(len(numbers)):
# If duplicate found, shrink window from left
while numbers[right] in seen:
seen.remove(numbers[left])
left += 1
# Add current element to set
seen.add(numbers[right])
# Update maximum length
max_len = max(max_len, right - left + 1)
return max_len
# Test with sample data
numbers = [1, 2, 3, 2, 4, 5]
result = longest_distinct_sublist(numbers)
print(f"Input: {numbers}")
print(f"Length of longest distinct sublist: {result}")
The output of the above code is ?
Input: [1, 2, 3, 2, 4, 5] Length of longest distinct sublist: 4
Using Dictionary to Track Positions
Another approach uses a dictionary to store the last seen position of each element. This allows us to jump directly to the position after the duplicate element instead of moving one step at a time.
Example
Here's how to implement this optimized approach ?
def longest_distinct_sublist_optimized(numbers):
last_seen = {}
left = 0
max_len = 0
for right in range(len(numbers)):
# If element seen before and within current window
if numbers[right] in last_seen and last_seen[numbers[right]] >= left:
left = last_seen[numbers[right]] + 1
# Update last seen position
last_seen[numbers[right]] = right
# Update maximum length
max_len = max(max_len, right - left + 1)
return max_len
# Test with different data
numbers = [5, 1, 3, 5, 2, 3, 4, 1]
result = longest_distinct_sublist_optimized(numbers)
print(f"Input: {numbers}")
print(f"Longest distinct sublist length: {result}")
The output of the above code is ?
Longest distinct sublist length: 5
How It Works
The sliding window technique maintains two pointers:
- Left pointer: Start of the current window
- Right pointer: End of the current window (current element)
When a duplicate is found, the left pointer moves to eliminate the duplicate from the current window. The maximum window size encountered gives us the longest distinct sublist length.
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Set with while loop | O(n) | O(min(n,k)) | Simple implementation |
| Dictionary optimization | O(n) | O(min(n,k)) | Better performance |
Note: k represents the number of unique characters in the input.
Conclusion
The sliding window approach efficiently finds the longest distinct sublist in O(n) time. Use the set-based method for simplicity or the dictionary method for optimal performance when dealing with large datasets.
