My c++ solution with map


  • 5
    H
     int majorityElement(vector<int> &num) {
            map<int, int>count;
            for (vector<int>::iterator i = num.begin(); i != num.end();i++) {
                if ( (++count[*i]) > num.size() / 2)
                    return *i;
            
            }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.