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
+1
itsupk2 hours ago
Moore's Voting Algorithm:
int majorityElement(int arr[], int size)
{
    int candidate=-1,vote=0;
    
    for(int i=0;i<size;i++)
    {
        if(vote==0)
        {
            candidate=arr[i];
            vote=1;
        }
        else if(arr[i]!=candidate)
        vote--;
        else vote++;
    }
    
    int cnt=0;
    
    for(int i=0;i<size;i++)
    {
        if(arr[i]==candidate)
        cnt++;
    }
    return cnt>(size/2)?candidate:-1;
    
    
}
-2
btwitssahil8 hours ago
int majorityElement(int a[], int size)
    {
        // your code here
        int num;
        unordered_map<int , int> m;
        for(int i=0; i<size; i++){
            m[a[i]]++;
        }
        for(auto i : m){
            if(i.second > size/2){
                
                return i.first;
            }
        }
        return -1;
       
    }
0
kaushaldeokar12 hours ago
int majorityElement(int a[], int n)   {          int maj=0,ct=1;       for(int i=1;i<n;i++){           if(a[maj]==a[i])ct++;           else ct--;           if(ct==0){               maj=i;               ct=1;           }       }       ct=0;       int cand=a[maj];       for(int i=0;i<n;i++)if(a[i]==cand)ct++;       if(ct>n/2)return a[maj];       return -1;   }
0
gitansh18saharan2 days ago
JAVA
 
class Solution{   static int majorityElement(int a[], int size)   {       // your code here       int ans =0;       int count =1;              for(int i =0;i<a.length;i++){                      if(a[ans] == a[i]){               count++;           }           else{               count--;           }           if(count == 0){               ans =i;               count=1;           }       }              // to check        int c=0;              for(int i =0 ; i< a.length ;i++){           if(a[ans]==a[i]){               c++;           }                  }       if(c > size/2 ){           return a[ans];       }       else{           return -1;       }   }}
-3
soumyajitbiswas122 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 c=size/2;      unordered_map<int, int> mp;      for(int i=0; i<size; i++)   {       mp[a[i]]++;   }      unordered_map<int, int>::iterator itr;      for(itr=mp.begin(); itr!=mp.end(); itr++)   {       if(itr->second > c)           return itr->first;   }   return -1;   }};
+1
shikharkannojia22222 days ago
Please OPTIMIZE THIS CODE
C++ 
 
int max=-1;      int count=0;       int ans=0;       for(int i=0;i<=size;i++){        count=0;             for(int j=i+1;j<=size;j++){               if(a[i]==a[j]){                   count++;               }                              if(count>max && count>1 && count>=size/2){                   max=count;                   ans=a[i];               }           }       }if(ans==0){           ans=-1;           if(size==1){               ans=a[0];           }       }   return ans;       
-3
ujjwaljaiswal90903 days ago
in cpp
 
int majorityElement(int a[], int size)   {              // your code here         unordered_map<int,int>mp;      for(int i=0;i<size;i++){       mp[a[i]]++;   }      for(int i=0;i<size;i++){       if(mp[a[i]]>size/2) return a[i];   }      return -1;          }
0
rushi21263 days ago
Simple Java Solution:-
 
static int majorityElement(int a[], int size)   {       // your code here       int count=1;       int maxIndex = 0;       for(int i=1; i<size; i++)       {           if(a[i]==a[maxIndex])           {               count++;           }           else           {               count--;           }                      if(count==0)           {               maxIndex = i;               count=1;           }       }              int candidate = a[maxIndex];       int countCand = 0;              for(int i=0; i<size; i++)       {           if(a[i]==candidate)           {               countCand++;           }       }       if(countCand>size/2)       {           return candidate;       }       else       {           return -1;       }          }
-3
sakshisuman983 days ago
V.EASY C++ SOLUTION USING HASHING
  int majorityElement(int arr[], int n)
    {
        
        unordered_map<int,int>majority;
        for(int i=0;i<n;i++)
        {
            majority[arr[i]]++;
        }
         for(int i=0;i<n;i++)
        {
           if(majority[arr[i]]>n/2)
           {
               return arr[i];
           }
        }
        
       return -1;    
    }
-3
aashishreddy853 days ago
int majorityElement(int a[], int size)   {          unordered_map<int,int> m;   for(int i=0; i<size; i++)       m[a[i]]++;       for(auto e : m){           if(e.second>size/2)          return e.first;                  }       return -1;          }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.