FastPrepLongest Subarray Without Repeated Values
Problem · Array

Longest Subarray Without Repeated Values

Learn this problem
MediumAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Given an integer array nums, return the maximum length of a contiguous subarray whose values are all distinct.

Function

longestDistinctSubarray(nums: int[]) → int

Examples

Example 1

nums = [1,2,3,1,2,3,4]return = 4

The suffix [1,2,3,4] has four distinct values.

Example 2

nums = []return = 0

The empty array has no non-empty subarray.

Example 3

nums = [5,5,5]return = 1

Any two adjacent values repeat, so a single element is optimal.

Constraints

  • 0 <= nums.length <= 2 * 10^5.
  • -10^9 <= nums[i] <= 10^9.

More Amazon problems

drafts saved locally
public int longestDistinctSubarray(int[] nums) {
  // write your code here
}
nums[1,2,3,1,2,3,4]
expected4
checking account