Python – Minimum identical consecutive Subarray
Last Updated :
04 May, 2023
Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element that has the minimum consecutive occurrence. The knowledge of solution to it can be of great help and can be employed whenever required. Let’s discuss a certain way in which this task can be performed.
Method 1: Using groupby() + min() + lambda This task can be solved using a combination of above functions. In this, we group each occurrence of numbers using groupby() and get the min of it using min(). The lambda function provide utility logic to perform this task.
Python3
from itertools import groupby
test_list = [ 1 , 1 , 1 , 2 , 2 , 5 , 5 , 5 , 5 ]
print ("The original list is : " + str (test_list))
temp = groupby(test_list)
res = min (temp, key = lambda sub: len ( list (sub[ 1 ])))
print ("Minimum Consecutive identical Occurring number is : " + str (res[ 0 ]))
|
Output :
The original list is : [1, 1, 1, 2, 2, 5, 5, 5, 5]
Minimum Consecutive identical Occurring number is : 2
Time Complexity: O(n) where n is the number of elements in the string list. The groupby() + min() + lambda is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is created.
Method 2: Use numpy library
Note: install numpy module using command “pip install numpy”
The numpy approach uses the unique() function from the numpy library to find the unique elements in the list and their indices and counts. It then finds the minimum count from the count array and uses that to find the corresponding element in the original list.
Python3
import numpy as np
test_list = [ 1 , 1 , 1 , 2 , 2 , 5 , 5 , 5 , 5 ]
_, index, count = np.unique(test_list, return_index = True , return_counts = True )
min_count = min (count)
min_element = test_list[index[count = = min_count][ 0 ]]
print ( "Minimum Consecutive identical Occurring number is :" , min_element)
|
Output:
Minimum Consecutive identical Occurring number is : 2
Time complexity: O(n) as it is iterating through the list once.
Space complexity: O(n) as it is storing count of each element in the dictionary.
Method 3 : Using the collections module
Python3
from collections import defaultdict
test_list = [ 1 , 1 , 1 , 2 , 2 , 5 , 5 , 5 , 5 ]
print ( "The original list is : " + str (test_list))
freq_dict = defaultdict( int )
for elem in test_list:
freq_dict[elem] + = 1
min_value = min (freq_dict.values())
for elem, count in freq_dict.items():
if count = = min_value:
min_elem = elem
break
print ( "Minimum Consecutive identical Occurring number is : " + str (min_elem))
|
Output
The original list is : [1, 1, 1, 2, 2, 5, 5, 5, 5]
Minimum Consecutive identical Occurring number is : 2
Time complexity: O(n) – where n is the length of the list, due to the loop to create the frequency dictionary.
Auxiliary space: O(k) – where k is the number of unique elements in the list, due to the frequency dictionary.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...