Count distinct elements in an array in Python Last Updated : 07 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given an unsorted array, count all distinct elements in it. Examples: Input : arr[] = {10, 20, 20, 10, 30, 10} Output : 3 Input : arr[] = {10, 20, 20, 10, 20} Output : 2 We have existing solution for this article. We can solve this problem in Python3 using Counter method. Approach#1: Using Set() This ap roach defines a function count_distinct that takes an array as input and returns the count of distinct elements in the array using Python's built-in set data structure. The set data structure is used to remove duplicates from the array and get only the distinct elements, and len function is used to get the count of those distinct elements. The program then calls this function with two different input arrays to demonstrate its functionality. Algorithm 1. Create an empty set.2. Traverse the given array, and add each element to the set.3. The size of the set at the end will give the count of distinct elements. Python3 def count_distinct(arr): distinct = set(arr) return len(distinct) arr = [10, 20, 20, 10, 30, 10] print(count_distinct(arr)) arr = [10, 20, 20, 10, 20] print(count_distinct(arr)) Output3 2 Time Complexity: O(n), where n is the number of elements in the array.Auxiliary Space: O(n), where n is the number of elements in the array (to store the set). Comment More infoAdvertise with us Next Article Count distinct elements in an array in Python S Shashank Mishra Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides 3 min read Counting the number of non-NaN elements in a NumPy Array In this article, we are going to see how to count the number of non-NaN elements in a NumPy array in Python. NAN: It is used when you don't care what the value is at that position. Maybe sometimes is used in place of missing data, or corrupted data. Method 1: Using Condition In this example, we wil 3 min read Python - Count the elements in a list until an element is a Tuple The problem involves a list of elements, we need to count how many elements appear before the first occurrence of a tuple. If no tuple is found in the list, return the total number of elements in the list. To solve this, initialize a counter and use a while loop to iterate through the list, checking 2 min read Count occurrences of an element in a list in Python A common task when working with lists is to count how many times a specific element appears. In Python, we have several ways to count occurrences of an element using both built-in and custom methods.The simplest and most straightforward way to count occurrences of an element in a list is by using th 2 min read Python | Count occurrences of an element in a Tuple In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple: 3 min read Like