Count how many distinct integers appear only within one contiguous block after sorting the array.
Given an integer array nums, consider the array after sorting it in nondecreasing order.
A value is said to appear in a single block if all of its occurrences are consecutive in the sorted array, and the occurrences of that value do not get split into multiple separated groups.
Return the number of distinct integers in nums that appear in exactly one contiguous block in the sorted order.
In practice, this means counting the values whose occurrences form one uninterrupted segment in the sorted array.
Input Format
- An integer array
nums. - The array may contain duplicates and can be unsorted.
Output Format
- Return an integer: the number of distinct values that appear in exactly one contiguous block in the sorted array.
Constraints
1 <= nums.length- Values are integers.
- Use a solution that is efficient for large arrays.
Example 1
Input
nums = [1,2,1,2,3,3]
Output
3
Explanation
After sorting, the array is [1,1,2,2,3,3]. Each distinct value appears in one contiguous block, so the answer is 3.
Example 2
Input
nums = [1,3,1,2,2,2,3]
Output
3
Explanation
After sorting, the array is [1,1,2,2,2,3,3]. Values 1, 2, and 3 each occupy one block, so the answer is 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.