Python | Maximum distance between elements

• Last Updated : 01 Mar, 2020

Sometimes, while working with lists, we can have a problem in which we need to find maximum distance between reoccurring elements. This kind of problem can have application in competitive programming and web development domain. Let’s discuss certain way in which this task can be performed.

Method : Using loop + max() + defaultdict() + enumerate()
The combination of above functions can be used to perform this particular task. In this, we first initialize the temp dict with list using defaultdict(). The iteration is using enumerate() and max() performs the maximum distance between all similar numbers in list.

 # Python3 code to demonstrate # Maximum distance between elements# using max() + enumerate() + loop + defaultdict()from collections import defaultdict  # Initializing listtest_list = [4, 5, 6, 4, 6, 3]  # printing original listprint("The original list is : " + str(test_list))  # Maximum distance between elements# using max() + enumerate() + loop + defaultdict()temp = defaultdict(list)for idx, ele in enumerate(test_list):    temp[ele].append(idx)res = max(temp[ele][-1]-temp[ele] for ele in temp)  # printing result print ("Maximum distance between same element is : " + str(res))
Output :
The original list is : [4, 5, 6, 4, 6, 3]
Maximum distance between same element is : 3

My Personal Notes arrow_drop_up