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:
n = int(input())# create a sorted list of the numbersnumbers = sorted(map(int, input().split()))ans = 1for 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 answerif numbers[i] != numbers[i - 1]:ans += 1print(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!