Python Program for Counting Distinct Elements in an Array

Counting Distinct Elements in an Array using Python

Count the distinct elements

Today we will explain you how to count distinct elements in an array with the help of Python programming. Distinct elements means the total count of the unique elements in a list.
For Example
Enter the array elements
66 12 1 2 57 34 2 66
The elements without duplicate
66 12 1 2 57 34
The count of distinct elements
6

Working needed to count distinct elements in an array

Step 1: Initialize arrays.

Step 2: Take the element of the array from the user.

Step 3: Now with the dict function remove duplicate elements from the list.

Step 4: Than count the elements with the len() function.

Step 5: Print the distinct counts of the elements of a array.

Count the distinct elements elements in an array using Python

arr=list(map(int,input("ENTER ARRAY ELEMENTS ").split()))
x=list(dict.fromkeys(arr)) #to remove duplicate in list
print("ELEMENTS OF ARRAY",*arr)
print("ELEMENTS OF ARRAY WITHOUT DUPLICATES ",*x)
print("COUNT OF DISTINCT ELEMENTS ",len(x))
Output
ENTER ARRAY ELEMENTS 4  5  66  23  4  72
ELEMENTS OF ARRAY 4  5  66  23  4  22  72
ELEMENTS OF ARRAY WITHOUT DUPLICATES 4  5  66 23  77
COUNT OF DISTINCT ELEMENTS 5