These problems evaluate proficiency with array manipulation, frequency and membership reasoning, and algorithmic analysis of time and space complexity when detecting duplicates and locating longest subarrays constrained by distinct values.
Solve the following array problems.
Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.
Input: nums: int[]
Output: bool
Constraints (typical):
0 <= nums.length <= 1e5
-1e9 <= nums[i] <= 1e9
You are given an array fruits where fruits[i] is the type of fruit at position i. Starting from any position, you can pick one fruit from each consecutive tree moving right, but you can carry at most two distinct fruit types total.
Return the maximum number of fruits you can pick (i.e., the length of the longest contiguous subarray that contains no more than 2 distinct values).
Input: fruits: int[] (or string[] depending on representation)
Output: int
Constraints (typical):
1 <= fruits.length <= 1e5