Python Counter | Majority Element Last Updated : 24 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Majority Element: A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element). Write a function which takes an array and emits the majority element (if it exists), otherwise prints NONE as follows: Examples: Input : 3 3 4 2 4 4 2 4 4 Output : 4 Input : 3 3 4 2 4 4 2 4 Output : NONE We have existing solution for this problem please refer Majority Element link. We can solve this problem quickly in Python using Counter(iterable) function. Approach is simple, Convert given list of elements into dictionary using Counter() method, having elements as keys and their frequencies as value. Now traverse complete dictionary and check for element whose frequency follows the condition greater than (n/2) where n is size of list. That element will be majority element. Python3 # Function to find majority element from collections import Counter def majority(arr): # convert array into dictionary freqDict = Counter(arr) # traverse dictionary and check majority element size = len(arr) for (key,val) in freqDict.items(): if (val > (size/2)): print(key) return print('None') # Driver program if __name__ == "__main__": arr = [3,3,4,2,4,4,2,4,4] majority(arr) Output: 4 Comment More infoAdvertise with us Next Article Python | Counter Objects | elements() S Shashank Mishra Follow Improve Article Tags : Python python-dict Practice Tags : pythonpython-dict Similar Reads Python | Counter Objects | elements() Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Python's general-purpose built-ins like dictionaries, lists, and tuples. Counter is a sub-c 6 min read Python | Counter Objects | elements() Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Python's general-purpose built-ins like dictionaries, lists, and tuples. Counter is a sub-c 6 min read 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 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 Python most_common() Function most_common() function is a method provided by the Counter class in Python's collections module. It returns a list of the n most common elements and their counts from a collection, sorted by frequency. Example:Pythonfrom collections import Counter a = ['apple', 'banana', 'apple', 'orange', 'banana', 2 min read Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the 3 min read Like