jayati

Majority Element

Feb 26th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int majorityElement(vector<int>& nums) {
  4.         int n=nums.size();
  5.         unordered_map<int,int> m;
  6.         for(int i=0;i<nums.size();i++)
  7.         {
  8.             m[nums[i]]++;
  9.         }
  10.         int mx = -1;
  11.         int mx_ele;
  12.         for(auto u:m)
  13.         {
  14.             if(u.second>mx)
  15.             {
  16.                 mx=u.second;
  17.                 mx_ele=u.first;
  18.             }
  19.         }
  20.         int ans = floor(n/2);
  21.         return mx_ele;
  22.  
  23.        
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment