Longest Distinct Span Problem

Longest Distinct Span Problem — ExecCode Medium DSA Practice

Solve the Longest Distinct Span problem on ExecCode. Free online medium DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.

Problem description

You are given an integer array a. A distinct span is a contiguous subarray in which every value appears at most once. Return the length of the longest distinct span. You are given: a — integers, which may repeat and may be negative Return the length of the longest contiguous subarray with all distinct values. One element is a distinct span of length 1. When a value repeats inside the current span, shorten the span from the left until that value appears only once. An O(n) solution is required.

Examples

Input {'a': [1, 2, 3, 1]}; Output 3. Input {'a': [1, 1, 1]}; Output 1. Input {'a': [1, 2, 1, 3, 2, 4]}; Output 4

Constraints

1 <= a.length <= 100000 1000000000 <= a[i] <= 1000000000

Practice Longest Distinct Span free on ExecCode. Browse DSA problems, topic map, and placement guides.