oneby7

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: 4

Explanation: 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: 1

Explanation: Every element holds the same value, 9.

Example 3

Input: nums = [-1000000000, 5, -5, 1000000000]

Output: 4

Explanation: 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.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.