Foundations Standard library
591. Count Distinct Values
Easy
You are given an array of integers nums. Return how many distinct values it holds.
Distinct means different. Each value counts once, however many times it appears, and equal values do not have to be next to each other.
Example 1
Input: nums = [4, -2, 4, 7, -2, 4, 0]
Output:
4Explanation: The distinct values are 4, -2, 7 and 0. The value 4 appears three times and -2 appears twice, but each of them counts once.
Example 2
Input: nums = [9, 9, 9, 9, 9]
Output:
1Explanation: Every element holds the same value, 9.
Example 3
Input: nums = [-1000000000, 5, -5, 1000000000]
Output:
4Explanation: No value repeats, so all four count. 5 and -5 are different values.
Check your understanding
Work out the answer for this input before you write any code.
nums = [5, 1, 5, 2, 1, 5]
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.