Problem · Array
Longest Subarray Without Repeated Values
Learn this problemProblem statement
Given an integer array nums, return the maximum length of a contiguous subarray whose values are all distinct.
Function
longestDistinctSubarray(nums: int[]) → intExamples
Example 1
nums = [1,2,3,1,2,3,4]return = 4The suffix [1,2,3,4] has four distinct values.
Example 2
nums = []return = 0The empty array has no non-empty subarray.
Example 3
nums = [5,5,5]return = 1Any two adjacent values repeat, so a single element is optimal.
Constraints
0 <= nums.length <= 2 * 10^5.-10^9 <= nums[i] <= 10^9.