Untitled

 avatar
unknown
plain_text
a year ago
344 B
4
Indexable
class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        unordered_map<int,int>mp;
        vector<int>res;
        int c=floor(nums.size()/3);

        for(auto i:nums) {
            mp[i]++;
            if(mp[i]==c+1) res.push_back(i); //more than c times so c+1
        }
        return res;
    }
};
Editor is loading...
Leave a Comment