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:
Input : 3 3 4 2 4 4 2 4 4 Output : 4 Input : 3 3 4 2 4 4 2 4 Output : NONE
- 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.
- Operations on Python Counter
- Python | Counter Objects | elements()
- Python | Case Counter in String
- Anagram checking in Python using collections.Counter()
- Python dictionary, set and counter to check if frequencies can become same
- Python Counter| Find all duplicate characters in string
- Dictionary and counter in Python to find winner of election
- Python | Minimum number of subsets with distinct elements using Counter
- Python Counter| Find duplicate rows in a binary matrix
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python Counter to find the size of largest subset of anagram words
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Python | Slicing list from Kth element to last element
- Python | Append Odd element twice
- Python | Indices of Kth element value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.