Educatifu
Open menu

Count Distinct Values

Mediumarrayshashing

Count how many different values appear in a list.

Input

Line 1: an integer n. Line 2: n integers.

Output

The number of distinct values.

Examples

Example 1

Input
5
1 2 2 3 3
Output
3

Distinct values are 1, 2, 3 — three of them.

Example 2

Input
4
7 7 7 7
Output
1

Only one distinct value.

Example 3

Input
3
1 2 3
Output
3

All different.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= each number <= 10^9

Hints

Reveal hint 1

A set keeps only one copy of each value.

Reveal hint 2

Insert every number into a set and report its size.

Reference solution (spoiler — try it yourself first)

Approach

Insert all values into a set; the answer is the set size.

import sys
d = list(map(int, sys.stdin.read().split()))
n = d[0]
print(len(set(d[1:1 + n])))

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation