Longest Run of Non-Repeating Numbers in an Array
Arrays & HashingAuto-graded · 18 test casesLast updated
Find the length of the longest contiguous stretch of an integer array in which no value appears twice, using a sliding window with a last-seen index map.
Longest Run Without Repeats
You are given a list of integers values. A run is a contiguous block of elements (a subarray). A run is repeat-free if no number occurs more than once inside it.
Return the length of the longest repeat-free run in values.
Rules
- The run must be contiguous: the elements must be adjacent in the original list, in their original order.
- If
valuesis empty, return0. - If every element is the same, the answer is
1. - If all elements are different, the answer is
len(values). - Only the length is returned, not the run itself.
Constraints
0 <= len(values) <= 10^5-10^9 <= values[i] <= 10^9- Aim for a solution that runs in roughly linear time.
Examples
Example 1
Input: longest_repeat_free_run([1, 2, 3, 1, 4, 5])
Output: 5
Example 2
Input: longest_repeat_free_run([7, 7, 7, 7])
Output: 1
Hints
Hint 1
Think of a window [left, right] that always contains distinct values. Extend right one step at a time and decide how far left must move when a duplicate shows up.
Hint 2
Store, for each value, the last index at which it appeared. When the new value's previous index is inside the window, move left to just after that index.
Hint 3
Be careful with stale entries: only move left if the previous index is at least left (or use left = max(left, last_index + 1)), so the left boundary never moves backwards.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use a sliding window [start, end] that is always repeat-free, together with a dictionary mapping each value to the most recent index where it appeared.
Move end across the list one element at a time. For the current number:
- If it was seen before and that earlier index is at least
start(so it lies inside the window), the window would contain a duplicate. Movestartto one past that earlier index. - Record the current index as the latest position of this number.
- The window is now repeat-free, so update the best length with
end - start + 1.
Why it is correct
After processing each index end, the window values[start..end] is the longest repeat-free run ending at end.
- Distinct: the only new duplicate that adding
values[end]can create is with an earlier copy of the same number. If that copy is inside the window, movingstartjust past it removes it. If it is beforestart, it is already outside the window and nothing needs to change. This is why the>= startcheck matters; without it,startcould move backwards (for example on[1, 2, 2, 1]). - Longest: any window ending at
endthat begins at or before the earlier copy would contain both copies. Also,startonly advances when forced to, so it is the smallest valid start.
Every subarray ends at some index, and we consider the best one for each ending index, so the maximum over all indices is the global answer.
Edge cases fall out naturally: an empty list never enters the loop and returns 0; all-equal input keeps the window at length 1; all-distinct input never moves start, giving len(values).
For [1, 2, 3, 1, 4, 5] the window ends as [2, 3, 1, 4, 5], so the answer is 5.
Complexity
- Time: O(n), since each element is visited once,
startonly moves forward, and dictionary operations are O(1) on average. - Space: O(d), where d is the number of distinct values (at most n).
Reference solution
from typing import List
def longest_repeat_free_run(values: List[int]) -> int:
last_index = {}
start = 0
best = 0
for end, number in enumerate(values):
if number in last_index and last_index[number] >= start:
start = last_index[number] + 1
last_index[number] = end
best = max(best, end - start + 1)
return best