Finding the Maximum Distance between Elements using Python

Finding the maximum distance between duplicate elements in a Python list is a common problem in data analysis. The distance refers to the difference between the highest and lowest index positions where the same element appears.

Problem Understanding

Given a list with duplicate elements, we need to find the maximum distance between any two occurrences of the same element. For example, in the list [1, 3, 5, 1, 5, 7, 8], the element 5 appears at indices 2 and 4, giving a distance of 2.

Using NumPy Module

NumPy provides efficient array operations to find unique elements and their positions ?

import numpy as np

# Initialize the list with duplicate elements
data = [1, 3, 5, 2, 1, 7, 8]

# Get unique elements
unique_elements = np.unique(data)

maximum_distance = 0

# Check each unique element
for element in unique_elements:
    # Find all positions where element occurs
    positions = np.where(np.array(data) == element)[0]
    
    # Calculate distance between first and last occurrence
    distance = np.max(positions) - np.min(positions)
    
    # Update maximum distance
    if distance > maximum_distance:
        maximum_distance = distance

print("Maximum distance:", maximum_distance)
Maximum distance: 4

Using Iteration Method

A pure Python approach using nested loops and list comprehension ?

def find_max_distance(data):
    max_distance = 0
    
    # Get unique elements by converting to set
    unique_elements = set(data)
    
    for element in unique_elements:
        # Find all indices where element occurs
        indices = [i for i, value in enumerate(data) if value == element]
        
        # Calculate distance between first and last occurrence
        if len(indices) > 1:
            distance = max(indices) - min(indices)
            max_distance = max(max_distance, distance)
    
    return max_distance

# Test the function
data = [4, 9, 6, 2, 9, 1]
result = find_max_distance(data)
print("Maximum distance:", result)
Maximum distance: 3

Using Dictionary Approach

Track first and last occurrence of each element in a single pass ?

def max_distance_dict(data):
    first_occurrence = {}
    last_occurrence = {}
    
    # Record first and last occurrence of each element
    for i, element in enumerate(data):
        if element not in first_occurrence:
            first_occurrence[element] = i
        last_occurrence[element] = i
    
    # Find maximum distance
    max_distance = 0
    for element in first_occurrence:
        distance = last_occurrence[element] - first_occurrence[element]
        max_distance = max(max_distance, distance)
    
    return max_distance

# Test the function
data = [1, 3, 5, 2, 1, 7, 5, 8]
result = max_distance_dict(data)
print("Maximum distance:", result)
Maximum distance: 5

Comparison

Method Time Complexity Space Complexity Best For
NumPy O(n²) O(n) Numerical data
Iteration O(n²) O(n) Small lists
Dictionary O(n) O(n) Large lists

Conclusion

The dictionary approach offers the best time complexity O(n) for finding maximum distance between duplicate elements. Use NumPy for numerical computations or the dictionary method for optimal performance with large datasets.

Updated on: 2026-03-27T11:18:10+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements