Skip to content
BytePatterns

Longest Distinct Run

MediumArrays#sliding-window#hash-map~25m

Problem

Given a list of values, find the length of the longest contiguous stretch in which no value repeats. The stretch has to stay in one unbroken run, so you cannot skip over an element to avoid a repeat. Return 0 when the list is empty.

Examples

Input:  items = [1, 2, 3, 2, 4, 5]
Output: 4
Why:    the run 3, 2, 4, 5 has no repeats
Input:  items = [7, 7, 7]
Output: 1
Why:    every pair repeats, so a run of one is the best possible
Input:  items = []
Output: 0
Why:    edge case, there is no run at all

Hints

0 / 3

Stuck on the idea rather than the code? Sliding Window covers it.