Majority Element | Practice | GeeksforGeeks

Given an array A of N elements. Find the majority element in the array. A majority element in an array A of size N is an element that appears more than N/2 times in the array.
 
Example 1:
Input:
N = 3 
A[] = {1,2,3} 
Output:
-1
Explanation:
Since, each element in 
{1,2,3} appears only once so there 
is no majority element.

Example 2:
Input:
N = 5 
A[] = {3,1,3,3,2} 
Output:
3
Explanation:
Since, 3 is present more
than N/2 times, so it is 
the majority element.


Your Task:
The task is to complete the function majorityElement() which returns the majority element in the array. If no majority exists, return -1.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 107
0 ≤ Ai ≤ 106
0
2016yashpratapin 6 hours
Beginner friendly...
 
class Solution{   static int majorityElement(int nums[], int size)   {       Arrays.sort(nums);       int count =1;       int n = nums.length-1;        if( size==1)            return nums[0];       for(int i=0;i<n;i++)       {           if(nums[i]==nums[i+1])           {               count++;               if(count>(size/2))                   return nums[i];           }           else           {               count = 1;           }       }       return -1;   }}
0
himanshusethin1 day ago
map<int,int> temp;       for(int i =0;i<size;i++){           auto itr = temp.find(a[i]);           if(itr == temp.end()){               temp.insert({a[i],1});                  itr = temp.find(a[i]);           }           else                itr->second = itr->second+1;           if(itr->second > size/2)               return itr->first;       }       return -1;
0
subhashishde081 day ago
#include<bits/stdc++.h>
int majorityElement(int a[], int size)
{
    int x = size/2;
    unordered_map<int,int> m;
    for(int i=0;i<size;i++){
        m[a[i]]++;
    }
    int res = -1;
    for(int i=0;i<size;i++){
        if(m[a[i]] > x){
            res = a[i];
            break;
        }
    }
    return res;
}
0
sangrambachu2 days ago
Based on Expected Space and Time Complexity we can get to know that we need to apply moore's algorithm for this problem.
 
class Solution
{
    static int majorityElement(int a[], int size)
    {
        // your code here
        int count = 1;
        int index = 0;
        
        for(int i=0; i<size; i++) {
            if(a[i] == a[index]) {
                count++;
            } else {
                count--;
            }
            
            if(count == 0) {
                index = i;
                count = 1;
            }
        }
        
        int element = a[index];
        int realCount = 0;
        for(int i=0; i<size; i++) {
            if(element == a[i]) {
                realCount++;
            }
        }
        
        if(realCount > (size/2)) {
            return element;
        } else {
            return -1;
        }
        
    }
}
0
lovesinghkalbhor2 days ago
class Solution{ public:    // Function to find majority element in the array   // a: input array   // size: size of input array   int majorityElement(int a[], int size)   {               int max = *max_element(a,a+size);  int arr[max+1] = {0};  for(int i = 0; i<size; i++){      arr[a[i]] += 1;  }  int smax  = *max_element(arr,arr+(max+1));  int i;  if(smax>size/2){      for(i = 0;i<max+1;i++){          if(arr[i] == smax){              return i;          }                }        }else{          return -1;      }          }};
0
abhishekdipu2 days ago
Javascript(Node) Solution : 
 
class Solution {    majorityElement (a, size){       let memo = {};              for (let i=0; i<size; i++){           if(!(a[i] in memo)){               memo[a[i]] = 1           }else{               memo[a[i]]++;           }           let freq = memo[a[i]];           if(freq > size/2) return a[i]       }       return -1   }}
0
kalaiabster2 days ago
JAVA SOLUTION : time taken  (3.06/4.59) 
 static int majorityElement(int a[], int size)   {       Arrays.sort(a);       int max=1,count=1,out=0;       if(size==1)         return a[0];         else       {       for(int i=1;i<a.length;i++)       {           if(a[i]==a[i-1])               count++;           if(a[i]!=a[i-1])               count=1;           if(count>max&&count>size/2)           {               max=count;               out=a[i];           }       }       if(max==1)       out=-1;       return out;       }   }
0
riteshthakare11913 days ago
For begineer:
int majorityElement(int a[], int size)   {       int ans=-1;   sort(a,a+size);   for (int i=0;i<size;i++){       int count=1;       while(i<(size-1) && a[i+1]==a[i]){           i++;           count++;       }       if (count>(size/2)){           ans=a[i];       }   }        return ans;   }
 
0
rohankurdekar06204 days ago
We can sort the array in c++ using library “algorithm”
and then traverse the array in O(n).
 
 int majorityElement(int a[], int size)   {      sort(a,a+size);      int i=0;      int c=1;      if(size<2)      {          return a[0];      }      for(i=0;i<size-1;i++)      {          if(a[i]!=a[i+1])          {              c=1;              continue;          }          else          {              c++;              if(c>size/2)              {                  return a[i];              }          }               }      return -1;         }
0
akasksingh0804 days ago
The optimal algorithm for this type of question is based on :
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.