Longest Distinct Subarray

easy
arrays sliding window hash table two pointers
🏢google
0 views0 likes

Problem Description

Find the length of the longest subarray within a given array where all elements are distinct.

Constraints:

  • The array contains integers.
  • The array's length is at least 1.

Examples:

Input: [7, 3, 5, 4, 5, 3, 6]

Output: 4 

Explanation: The longest subarray of distinct elements is [3, 5, 4, 5] or [5, 4, 5, 3]. Both have a length of 4.

Input: [2, 2, 2, 2, 2]

Output: 1

Explanation: Every element is the same, so the longest subarray of distinct elements is any single element, with a length of 1.

Hints

[object Object]

[object Object]

[object Object]