Distinct Elements Count
MediumPractice library · +20 XPgreedyhash mapcounting
Given an array of N integers, find the number of distinct (unique) elements present in the array. The order of elements does not matter.
Input
The first line contains an integer N, the number of elements. The second line contains N space-separated integers, representing the elements of the array.
Output
Print a single integer, the count of distinct elements.
Constraints
1 <= N <= 200000 -10^9 <= elements <= 10^9
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 2 2 3 1
Output
3
The distinct elements are 1, 2, and 3. The count is 3.
Example 2
Input
7 5 5 5 5 5 5 5
Output
1
All elements are the same, so there is only 1 distinct element.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 2 3 1
Example 2 input
7 5 5 5 5 5 5 5