Greatest Distance between Equal Numbers in Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computational problem-solving, one common task involves analyzing arrays to find patterns or specific characteristics. An intriguing challenge is determining the greatest distance between equal numbers within an array. This problem blends simple iteration concepts with efficient data handling, making it a popular topic in algorithm design and data structure courses.
Problem Definition
Given an integer array `arr`, the objective is to find the maximum distance between any two instances of the same number. The "distance" here refers to the number of indices between the first occurrence and the last occurrence of any given number.
Example
Consider the array `[1, 2, 3, 1, 4, 2]`.
- The number `1` appears at indices `0` and `3`. The distance is `3 - 0 = 3`.
- The number `2` appears at indices `1` and `5`. The distance is `5 - 1 = 4`.
- The number `3` appears only once at index `2`.
From this, the greatest distance between equal numbers is `4`, coming from the number `2`.
Technical Approach
To solve this problem efficiently, a direct solution with time complexity `O(n^2)` (by checking all pairs) is not practical for large arrays. Instead, a more optimal approach involves using a hash map (or dictionary in Python) to track indices:
Steps
- Initialize a HashMap: The key will be the number, and the value will be a list storing the first and last occurrence indices.
- Traverse the Array:
- For each element, if it is not in the hash map, add it with its current index as both the first and last occurrence.
- If it is already in the map, update the value only for the last occurrence index.
- Calculate Maximum Distance:
- Iterate over the hash map and compute the distance as `last_index - first_index` for each entry.
- Track the maximum of these distances.
Code Implementation
Here is a Python implementation:
- Time Complexity: The algorithm runs in `O(n)` time, where `n` is the number of elements in the array. Each element is processed once, making this solution linear in scale.
- Space Complexity: It requires additional space to store up to `n` unique elements in the hash map, leading to `O(n)` space complexity.
- Data Analysis: When exploring patterns in datasets, identifying the maximal spread of repeating elements can be vital, such as analyzing customer purchase cycles.
- IoT Systems: In scenarios like sensor data streams, determining signal consistencies or anomalies over time can utilize this maximum distance calculation.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.