Back to Solutions

Distinct Numbers

Explanation

Here, in this problem we are told to count the number of distinct values in a given list. We can do this by using a set. We can add all the values in the set and then return the length of the set.

But.. using a set in Python would exceed the time limit. So, we can first input the list, then sort it and use a for loop to check if the current value is equal to the next value. If it is not, then we can increment the count by 1. If it is, then we can continue. We can return the count at the end.

Code

n = int(input())
arr = sorted(list(map(int, input().split())))

count = 1
last = arr[0]
for e in arr[1:]:
    if e != last:
        count += 1
        last = e

print(count)