Find majority element in an array

Difficulty: Medium, Asked-in: Microsoft, Google, Amazon, Yahoo, Samsung

Key Takeaways

  • An excellent problem to learn problem-solving using various approaches and step-by-step time and space complexity optimization.
  • The Boyer-Moore Voting algorithm is worth exploring, which uses fascinating insight from the problem to get in-place solution using single loop and variables.

Let’s understand the problem

You are given an array X[] consisting of n elements, write a program to find the majority element in an array i.e. return the number which appears more than n/2 times.

  • Assume that array is non-empty and the majority element always exists.
  • A majority element appears more than n/2 times, so there is at most one such element.

Examples

Input: X[] = [2, 12, 2, 2, 2, 4, 2], Output: 2

Explanation: 2 is the majority element which appears 5 times.

Input: X[] = [4, 3, 4], Output: 4

Input: X[] = [1, 1, 1, 1, 1, 1], Output: 1

Discussed solution approaches

  • A brute force approach  using nested loops
  • Using sorting
  • Using divide and conquer approach
  • Using hash table
  • Using bit manipulation approach
  • Using randomized algorithm
  • An efficient single loop solution using the Boyer-Moore voting algorithm

A brute force approach  using nested loops

Solution Idea

A basic approach would be to check whether each element is the majority element or not. We can use two nested loops where outer loop iterates over array and inner loop count occurrences of each element. If we found an element with frequency greater than n/2, that’s our majority element.

Solution Pseudocode

int getMajority(int X[], int n)
{
    for (int i = 0; i < n; i = i + 1)
    {
        int majorityCount = 1
        for (int j = 0; j < n; j = j + 1)
        {
            if (X[i] == X[j])
               majorityCount = majorityCount + 1
        }
        if (majorityCount > n/2)
            return X[i]
    }
}

Solution Analysis

We are running two nested loops where each loop is running n times. Time complexity = O(n²), Space complexity = O(1). Can we improve time complexity further? Think!

Using sorting

Solution Idea

Given in the problem statement, the majority element always exists in array. So if we sort the array in increasing order, then the majority element will get grouped on adjacent indices. Can we access the majority element directly in sorted array? Think!

If we divide sorted array into two halves, each part contains n/2 elements each. We also know that the majority element repeated more than n/2 times in the array. Therefore, if the majority element is present in array, it must be present at the middle index. This is true for the cases when n is odd, and n is even.

Solution Pseudocode

int getMajority (int X[], int n) 
{
    sort(X, n)
    return X[n/2]
}

Solution Analysis

Suppose we are using an efficient O(nlogn) sorting algorithm heap sort. Time complexity = Time complexity of heap sort + Time complexity of accessing the middle element = O(nlogn) + O(1) = O(nlogn)

Space complexity = O(1), Heap sort is an in-place sorting algorithm.

Using divide and conquer approach

Solution Idea

Can we apply divide and conquer approach? Here is an insight: If we divide array into two halves and recursively find the majority element of left and right halves, we can easily determine the overall majority element in linear time. Think!

Solution Steps

We define a helper function getMajority(int X[], int l, int r) which takes left and right ends of array as a parameter. Initial value of l = 0 and r = n - 1. 

Base case: This is the trivial case of single element array when both left and right ends are equal. We return this single element as the majority element i.e. if (l == r), return X[l] or X[r]. 

Divide step: We divide array into two equal halves by calculating mid-value, i.e., mid = l + (r - l)/2

Conquer step: We recursively calculate the majority element of left and right halves and store them in variables leftMajority and rightMajority.

  • int leftMajority = getMajority (X, l, mid)
  • int rightMajority = getMajority (X, mid + 1, r)

Combine step: If majority elements of left and right halves are equal, then it must be the global majority element and we return this value. Think!. Otherwise, one of them must be equal to the global majority element. To determine this, we count the frequency of leftMajority and rightMajority in input array and return value with maximum frequency as the final output.

if (leftMajority == rightMajority)
    return leftMajority      
int leftCount = countFrequency(X, l, r, leftMajority)
int rightCount = countFrequency(X, l, r, rightMajority)
if(leftCount > rightCount)
    return leftMajority
else 
    return rightMajority

Solution Pseudocode

int getMajorityElement (int X[], int n) 
{
    return getMajority(X, 0, n-1)
}
 
int getMajority (int X[], int l, int r) 
{
    if (l == r)
        return X[l]
    int mid = l + (r - l)/2
    int leftMajority = getMajority (X, l, mid)
    int rightMajority = getMajority (X, mid + 1, r)
    if (leftMajority == rightMajority)
        return leftMajority
    int leftCount = countFrequency(X, l, r, leftMajority)
    int rightCount = countFrequency(X, l, r, rightMajority)
    if(leftCount > rightCount)
        return leftMajority
    else 
        return rightMajority
}

countFrequncy(X, l , r, majority) implementation

int countFrequency (int X[], int l, int r, int majority) 
{
    int count = 0
    for (int i = l; i <= r; i = i + 1)
    {
        if (X[i] == majority) 
            count = count + 1
    }
    return count
}

Solution Analysis

We are solving problem size n by recursively solving two smaller sub-problem of size n/2 and combing the solution of these two smaller problems by counting frequency of leftMajority and rightMajority (by performing two linear scans of size n).

  • Time complexity of divide part = O(1)
  • Time complexity of conquer part = 2T(n/2)
  • Time complexity of combine part = Time complexity of counting the frequency of leftMajority + Time complexity of counting the frequency of rightMajority = O(n) + O(n) = O(n)
  • Overall time complexity T(n) = O(1) + 2T(n/2) + O(n) = 2T(n/2) +O(n)

So time complexity can be represented by the following recurrence relation :

T(n) = 2 T(n/2) + O(n), if n > 1

T(n) = O(1), if n = 1

We can use the master theorem or recursion tree method to solve this recurrence relation. If we observe closely, this is similar to merge sort recurrence. Time complexity = O(nlogn).

Space complexity = O (logn) for recursion call stack. The divide and conquer method does not explicitly allocate any additional memory, but it uses an additional memory in stack frames due to recursion. The stack frame size depends on the max depth of the recursion tree, which is equal to O(logn) here. Think!

Using hash table

Solution Idea

In brute force approach, we are counting the frequency of each element using inner loop. Can we remove this inner loop and count frequency in O(1)? Yes, the idea would be to use hash table to count and store frequency of each distinct element. The hash table allows us to access and insert elements in O(1) on average.

Solution Steps

  1. We create hash table to store frequency for each distinct element. We store this as a key-value pair, i.e. (element, frequency) pair. Initially, all the slots in hash table will be initialized with 0.
  2. Now we traverse array from i = 0 to n - 1 to search X[i] in the hash table.
  3. If X[i] is not present, we insert it into the hash table and update frequency equal to 1. Otherwise, we increase its frequency count by 1.
  4. Now we check the frequency of element X[i] in the hash table. If frequency count is greater than n/2, we return X[i] as the majority element.

Solution Pseudocode

int getMajority (int X[], int n)
{
   HashTable H
   for (int i = 0; i < n; i = i + 1)
   {
        if (H.search(X[i]) == true)
            H[X[i]] = H[X[i]] + 1
        else
            H[X[i]] = 1
        
        if (H[X[i]] > n/2)
            return X[i]
    }
}

Solution Analysis

We are traversing array once and performing constant time hash table operations on each iteration. Therefore, time complexity = n*O(1) = O(n). Space complexity = O(n) for storing elements in hash table.

Using bit manipulation approach

Solution Idea

We can use mathematical insights from the binary representation of the numbers to find the majority element. How? Let's think.

Assume that each element is a 32-bit integer. As given in the problem, we also know that there is a majority element in the array. Now for all numbers, we count the frequency of set bits i.e. count the frequency of 1's at each bit position in the bitwise representation (It's like polling for each bit position).

At any position in bitwise representation:

  • If frequency of 1's is in majority (greater than n/2), then bit value at same position in the majority element will be 1.
  • If frequency of 1's is not in majority (less than n/2), then bit value at same position in the majority element will be 0.

For example, X[ ] = [6, 13, 13, 2, 13], n = 5, let's assume our input is 4-bit integer.

6  ->      0 1 1 0

13 ->      1 1 0 1

13 ->      1 1 0 1

2  ->      0 0 1 0

13 ->      1 1 0 1
-----------------------
Majority = 1 1 0 1 = 13

In the above example, let's start from left to right. 1's are in the majority at 1st, 2nd, and 4th position, so set bits at similar position in the majority element would be 1. At 3rd position, number of 1's across all array elements is only 2, not a majority, so it is set to 0 in the majority element.

So the idea would be simple: By identifying majority bits at every bit position for all the numbers, we can construct the majority element bitwise. How do we implement it? Let's think.

Solution Steps

  1. We initialize a variable to store the majority element, i.e., majorityElement = 0. Initially, all bits in majorityElement would be 0.
  2. Now we run a loop from currBit = 0 to 31, where loop variable currBit represents the current bit position of a 32-bit integer.
  3. For each bit position, we initialize a variable to count the frequency of 1's i.e. countOnes = 0.
  4. Now we iterate over entire array and increase countOnes by 1 if current bit position is 1 in an element i.e.

    for(i = 0; i < n; i = i + 1)
    {
      if((X[i] & (1 << currBit)) != 0)
          countOnes = countOnes + 1
    }
  5. By the end of loop, if countOnes value is greater than n/2, we set current bit position as 1 in variable majorityElement. Otherwise, we leave it because bit at that position is already set 0 in the majorityElement.

    if(countOnes > n/2)
      majorityElement = majorityElement + (1 << currBit)
  6. By the end of loop, we return value stored in majorityElement.

Solution Pseudocode

int getMajority(int X[], int n)
{
    int majorityElement = 0
    for (int currBit = 0; currBit < 32; currBit = currBit + 1)
    {
        int countOnes = 0
        for (int i = 0; i < n; i = i + 1)
        {
            if ((X[i] &(1<<currBit)) != 0)
                countOnes = countOnes + 1
        }
        
        if (countOnes > n/2)
            majorityElement = majorityElement + (1 << currBit)
    }
    
    return majorityElement
}

Solution Analysis

We are running two nested loops where inner loop is running n times and outer loop is running 32 times or constant time. At each iteration of nested loops, we are doing constant number of operations. Time complexity = 32 * O(n) * O(1) = O(n).

Space complexity = O(1), we are using constant number of variables only.

Using randomized algorithm

Solution Idea

Here is the intuition of randomized algorithm :  More than n/2 elements are equal to the majority element and a randomly picked element has more than 50% probability of success. So, we just select a random element in the array and check whether it is majority element or not. Return that element if it is, otherwise, keep repeating the same process. 

There are some critical questions : What is the exact probability that algorithm will find repeated elements? What is the running time of the algorithm? How a randomized algorithm is a good choice? Think! We can get an answer to this question in the analysis section.

Solution Pseudocode

int getMajorityElement(int X[], int n) 
{
    while (true)
    {
        int randomElement = X[randRange(0, n-1)]
        int k = countFrequency(X, randomElement, n)
        if (k > n/2)
            return randomElement
    }
}

int countFrequency (int X[], int random, int n) 
{
    int count = 0
    for (int i = 0; i < n; i = i + 1) 
    {
        if (X[i] == random)
            count = count + 1
    }
    return count
}

Solution Analysis

  • There are n choices for picking an element and out of these choices, the chances of success is n/2 + 1 in the worst case. Suppose every choice is equally likely, then probability of success is = (n/2 + 1)/n = 1/2 + 1/n > 1/2 (for n > 0). We can say in other words, the probability of failure is at most 1/2. 
  • For reducing the probability of failure, we are repeating algorithm again, if we don’t succeed. Each time we are making independent random choices. If we repeat algorithm 10 times, then probability of failure < (1/2)¹⁰
  • If we repeat the algorithm logn times, then the probability of failure < (1/2)^logn = 1/(n^log 2) = 1/n. In other words, algorithm succeeds with high probability i.e. at least (1 – 1/n). If the value of n is very large, this algorithm can produce the majority element in O(nlogn) time complexity with a high probability of success. Note: Time complexity of each trial is O(n). If we repeat the trial logn times, time complexity = n*O(logn) = O(nlogn).
  • If we repeat algorithm n times, the probability of failure < (1/2)^n = 1/(2^n). In other words, the algorithm succeeds with high probability i.e. at least 1 – 1/(2^n) where c > 0.

Note: The idea of “with high probability” is often used in the analysis of the randomized algorithms, where the “high probability” is with respect to the input size n.

An efficient approach: The Boyer-Moore voting algorithm

Solution Idea

Here is an intuition behind this algorithm: Since majority element occurs more than n/2 times, its frequency is greater than all other elements combined. Therefore, if we mark the occurrence of majority element as +1 and occurrence of any other element as -1, then overall sum of them would be definitely greater than zero. Think!

Another interesting analogy to understand this algorithm: Suppose we have n number of people, each holding one element of the array. Then, whenever two people find each other that neither holds the same array element as the other, they sit down. Eventually, in the end, if anyone is left standing, then that element is the majority element. Since majority element occurs more than n/2 times, we can guarantee that this approach will always find the majority element.

Solution Steps

  • To keep track of our current guess of the majority element, we declare a variable majorityCandidate and maintain a variable counter count. Initially, value of both variables are equal to 0.
  • Let’s walk across the array. If current element matches our guess, we increment count by 1. If current element doesn’t match our guess, we decrement the count by 1. Think!
  • If value of count is equal to 0, we reset the current guess and make it equal to the current element. i.e majorityCandidate = X[i]. In other words, we forget about everything up to the previous index and consider current element as a potential candidate for the majority element. 
  • By the end of loop, the value of the majority element gets stored in variable majorityCandidate. 

There are many ways to think about why this algorithm works. One good intuition is to think of this algorithm as breaking input into a chunk of consecutive copies of particular values. Incrementing the counter corresponds to marking multiple copies of the same value while decrementing it corresponds to some other sequences of values, “canceling out” accumulation of values of a particular type.

Note: As long as the majority element occurs with a frequency of at least n/2, we can guarantee that this approach will always find the majority element.

Solution Pseudocode

int majorityElement(int X[], int n) 
{
    int count = 0
    int majorityCandidate = 0
    for (int i = 0; i < n; i = i + 1) 
    {
        if (count == 0) 
            majorityCandidate = X[i]
        if (X[i] == majorityCandidate) 
            count = count + 1
        else 
            count = count - 1
    }
    return majorityCandidate
}

Solution Analysis

We are running a single loop n time and performing a constant operation at each step of the iteration. Time complexity = O(n). Space complexity = O(1), because we are using constant extra space.

Critical ideas to think about!

  • How do we modify the solution approaches if the majority element does not always exist in the array?
  • Can this problem be solved using a binary search tree? What will be the time and space complexity?
  • How do we implement the randRange() function in the 6th approach?
  • Prove the correctness of the Boyer-Moore Majority Vote Algorithm.
  • What is the best and worst-case space complexity in the 4th approach?

Comparisons of time and space complexities

  • Two nested loops: Time = O(n^2), Space = O(1)
  • Using sorting: Time = O(nlogn), Space = O(1)
  • Divide and conquer: Time = O(nlogn), Space = O(1)
  • Hash table: Time = O(n), Space = O(n)
  • Bit manipulation: Time = O(n), Space= O(1)
  • Randomized algorithm: Time = O(logn), with a probability of failure 1/n if n is very large, Space = O(1)
  • Boyer-Moore voting algorithm: Time = O(n), Space = O(1)

Suggested coding questions to practice

  • Find if any element repeats more than n/3 times
  • Check if an array has a majority element
  • n Repeated element in 2n size array
  • Find the majority element in a circular array of 0’s and 1’s
  • Sort elements based on their frequency
  • Find the element with the second-largest frequency

Enjoy learning, Enjoy algorithms!

Share on social media:

More blogs to explore

Our weekly newsletter

Subscribe to get weekly content on data structure and algorithms, machine learning, system design and oops.

© 2022 Code Algorithms Pvt. Ltd.

All rights reserved.