Bob has an array of n integers. Output the number of distinct values in the array . Input The first input line has an integer n: the number of values. The second line has n integers x1,x2,…,xn. Output Print one integers: the number of distinct values. Constraints 1≤n≤2⋅10*5 1≤xi≤10*9
时间: 2024-03-12 18:43:22 浏览: 47
Array_Of_Int.rar_The Request
To solve this problem, we can use a set to keep track of all the distinct values in the array. We can iterate through the array and add each element to the set. At the end, the size of the set will give us the number of distinct values.
Here's the Python code to solve the problem:
```
n = int(input())
arr = list(map(int, input().split()))
distinct_values = set()
for i in range(n):
distinct_values.add(arr[i])
print(len(distinct_values))
```
This code reads in the input, creates an empty set called `distinct_values`, and then iterates through the array `arr`, adding each element to the set. Finally, it prints the size of the set using the `len()` function.
阅读全文