Naive solution would be to count frequency of each element present in the first half of the array to check if it is majority element or not. Below is the naive implementation –

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

intmajorityElementNaive(intA[],intn)

{

// check if A[i] is majority element or not

for(inti=0;i<=n/2;i++)

{

intcount=1;

for(intj=i+1;j<n;j++){

if(A[j]==A[i]){

count++;

}

}

if(count>n/2){

returnA[i];

}

}

return-1;

}

The time complexity of above solution is O(n2).

We can improve worst case time complexity to O(nlogn) by sorting the array and then perform binary search for first and last occurrence of each element. If difference between first and last occurrence is more than n/2, we have found majority element.

O(n) solution –

We can use hashing to solve this problem in linear time. The idea is to store each element’s frequency in a map and return the element if its frequency becomes more than n/2. If no such element is present, then majority element does not exists in the array and we return -1. The time complexity of above solution is O(n) and auxiliary space used by the program is O(n).

Boyer–Moore majority vote algorithm –

We can find the majority element using linear time and constant space using Boyer–Moore majority vote algorithm. The algorithm can be expressed in pseudocode as the following steps:

Initialize an element m and a counter i = 0

for each element x of the input sequence:
if i = 0, then
assign m = x and i = 1
else
if m = x, then assign i = i + 1
else
assign i = i – 1

return m

The algorithm processes the each element of the sequence, one at a time. When processing an element x,

If the counter is 0, we set the current candidate to x and we set the counter to 1.

If the counter is not 0, we increment or decrement the counter according to whether x is the current candidate.

At the end of this process, if the sequence has a majority, it will be the element stored by the algorithm. If there is no majority element, the algorithm will not detect that fact, and will still output one of the elements. We can modify the algorithm to verify that the element found is really is a majority element or not.

Here is also possible the following approach:
1. remove pairs of different elements from array
2. the element that still exists is a candidate
3. check majority of candidate, should be in len(arr) // 2 elements

Thanks Sachin for sharing your concerns. The assumption is valid input i.e. majority element is present. As already mentioned if there is no majority element, the algorithm will not detect that fact, and will still output one of the elements.

For the O(nlogn)-solution, if you sort the array anyway, there is no need to check first and last occurrence of each element – just for the element in the middle. If there are more than n/2 occurrences of i, the middle element must be i.

Maybe another linear-time solution that uses constant extra memory? If there is a majority element, I think it would need to appear at index floor(n/2) in the sorted version of the array. Use quickselect to find that element. Then do a linear pass to verify that this element is indeed a majority.

True, if there is a majority element, the median is equal to it. Quickselect only has expected linear time, but does work in-place (if one deals with tail-recursion to eliminate the function stack).

The Blum-Floyd-Pratt-Rivest-Tarjan algorithm finds the median in O(n) deterministic time. One can rather easily modify this algorithm to work with no additional arrays, “in-place”. [One would still have the function stack of height log(n) for the recursion. But think there was a paper that deals with that, too!]