Javatpoint Logo
Javatpoint Logo

Majority Element

Majority Element in an Array using Python


Finding the majority element, which appears more than half the way down the array, is a fundamental challenge in array manipulation. Even though there are many ways to approach this issue, the divide and conquer algorithm stands out for its effectiveness and grace. This article will go in-depth on the Python divide-and-conquer algorithm for determining the majority element in an array.

Python Arrays

Python arrays are a useful data structure for effectively storing homogeneous data. They are especially helpful for numerical computations and situations where memory efficiency is crucial because of their fixed size and quick element access. Understanding and utilising arrays can significantly improve your Python programming abilities, whether you work with simple NumPy arrays or more complex ones.

What is the Majority Element?

An element that makes up more than half of an array's total size is considered to be the majority element. Formally, the majority element must appear more than n/2 times in an array of length n. Since it depends on the array already having a majority element, its existence is not guaranteed for all arrays.

The Majority Element Found Using a Dictionary

A well-liked and effective method for resolving the majority element problem in Python is to locate the majority element using a dictionary. The method entails storing the instances of each element in the array in a dictionary (or hash map). We can fill the dictionary and determine the majority element by iterating through the array just once.

Using Moore's Voting Algorithm

Moore's Voting Algorithm is another effective method for locating the majority element. This algorithm is based on the observation that the majority element will still exist if we cancel out every instance of an element with every other element that differs from it.

Finding the majority element in an array is straightforward using the Boyer-Moore Voting Algorithm, which has linear time complexity (O(n)) and constant space complexity. This is how the algorithm operates:

  1. 'Candidate' and 'count' should both be initialised. The potential majority component will be kept in the "candidate," while the current candidate's frequency will be tracked by the "count."
  2. Go through the array one by one, saying for each element:
    • Set the current element as the "candidate" and "count" to 1 if "count" is zero.
    • Increase "count" by 1 if the current element and the "candidate" match.
    • Decrease "count" by 1 if the current element differs from the "candidate."
  3. The 'candidate' will contain the potential majority element after iterating through the entire array.
  4. Count the instances of the 'candidate' element in the array to see if it really is the majority element. It is the majority element if it appears more than (n/2) + 1. The array would not contain a majority element otherwise.

Python implementation of the majority element algorithm

Use a dictionary to implement the majority element algorithm by performing the following actions:

  • Make an empty dictionary to keep track of each element's count.
  • Update the dictionary's count for each element as you traverse the array.
  • The element with the highest count should be located.
  • To determine if an element is the majority element, see if its count is greater than half the array's size.



Sample Array: [3, 2, 3, 4, 3, 2, 3, 5, 3]
Majority Element: 3

This code defines a function called find_majority_element that is used to determine the majority element in a given array. The majority element is defined as an element that appears more than half the number of times in the array. If such an element exists, the function returns it; otherwise, it returns None.

Here's a concise explanation of the code:

  1. The function find_majority_element(arr) takes a single argument: arr, which is the input array.
  2. A dictionary called element_count is created to keep track of the count of each element in the array.
  3. The code iterates through each number in the array. For each number, it updates its count in the element_count
  4. The majority element is determined by finding the element with the highest count using the max() function and passing a custom key function that retrieves the count of each element.
  5. After finding the majority element, the code checks if its count is greater than half the length of the array (len(arr) // 2). If this condition is met, the majority element is returned; otherwise, None is returned to indicate that there is no majority element.
  6. An example usage is provided with the sample array sample_array = [3, 2, 3, 4, 3, 2, 3, 5, 3]. The find_majority_element function is called with this array, and the resulting majority element is printed if one exists. If there is no majority element, a message indicating that no majority element was found is printed instead.

Analysis of Time Complexity

O(n), where "n" is the size of the input array, is the time complexity of the Moore's Voting Algorithm. Once through the array, constant-time operations are carried out on each element.

Since the verification step counts the instances of the majority_candidate, it also has an O(n) time complexity. However, following the initial traversal, this step is only carried out once.

Advantages of Moore's Voting Algorithm

  • The algorithm is very effective and only needs to pass once through the array.
  • Since it only needs two variables to track the majority candidate and its count, it uses constant space.
  • The algorithm's linear time complexity makes it effective for handling large datasets.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA