Maximum distance between equal values

Write codeMedium14 min

Your task

Given a list of integers, find the maximum distance between two equal values: the largest j - i over all index pairs i < j with nums[i] == nums[j]. Return 0 when no value occurs more than once (including empty and single-element lists). For example, in [3, 7, 3, 7, 3] the two outer 3s are 4 apart, so the answer is 4; in [4, 1, 4, 1, 1, 4] the answer is 5. A nested scan over all pairs is O(n^2), one hash map and one pass is enough.

Examples

max_equal_spread(nums: list[int]) -> int
Example 1
Inputmax_equal_spread([3, 7, 3, 7, 3])
Output4