Problem 202
Count Distinct Elements
Easy · 10 pts · Array, Hash Table
Problem
You are given an array of N integers. Print how many different values it contains, counting each value once no matter how often it appears. Add every element to a hash set and print the size of the set. Alternatively, sort the array and count the positions where an element differs from the one before it.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the number of distinct values.
Constraints
1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000
Example input
5 1 2 2 3 1
Example output
3
Explanation
In 1 2 2 3 1, the values 1 and 2 each appear twice and 3 appears once. The different values are 1, 2 and 3, so the answer is 3.