Untitled

majority
mail@pastecode.io avatar
unknown
c_cpp
3 years ago
487 B
2
Indexable
Never
 int majorityElement(vector<int>& nums) {
        unordered_map<int , int> m;
        int n = nums.size();
        
        vector<int> :: iterator it;
        
        for(it = nums.begin(); it != nums.end(); it++){
                m[*it]++;            
        }
        
        for(it = nums.begin(); it != nums.end(); it++){
            if(m[*it] > n/2){
                return *it;
                break;
            }            
        }
        return n;
    }