Problem · Array
Count Distinct Values in a Sorted Array
Learn this problemProblem statement
Given a nondecreasing integer array nums, return the number of distinct values in the array.
As a follow-up, let k be the number of distinct values. Design an approach that can skip long runs of duplicates and runs in O(k log n) time.
Function
countDistinct(nums: int[]) → intExamples
Example 1
nums = [-3,-3,-1,2,2,2,8]return = 4The distinct values are -3, -1, 2, and 8.
Example 2
nums = []return = 0An empty array contains no distinct values.
Constraints
0 <= nums.length <= 200000-10^9 <= nums[i] <= 10^9numsis sorted in nondecreasing order.