Method 1 - Sorting

Sort the array of numbers. Loop through the array and increment the answer for every distinct number. Distinct numbers can be found if the current number isn't equal to the previous number in the array.

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N\log{N})

n = int(input())
# create a sorted list of the numbers
numbers = sorted(map(int, input().split()))
ans = 1
for i in range(1, n):
# if the current number is different from the previous
# it is a distinct number so we add 1 to the answer
if numbers[i] != numbers[i - 1]:
ans += 1
print(ans)

Method 2 - Sets

See this module.

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!