January 09, 2025 |1.9K Views

Max Distance Between Two Occurrences

Explore Courseexplore course icon
Description
Discussion

Find the maximum distance between two occurrences of any element in an array. If no element appears more than once, return 0. The solution can be implemented using a brute-force approach with O(n^2) time complexity or a more efficient approach using a hash map with O(n) time complexity. The hash map approach stores the first occurrence of each element and calculates the distance for subsequent occurrences. 

For more details, read the full article here.