Only 2 lines C++ code


  • -11
    X

    This question is not so hard. We know that only the majority no. has n/2 times. So after sorting the middle value in this vector is the result.

    class Solution {
    

    public:
    int majorityElement(vector<int> &num) {
    sort(num.begin(),num.end());
    return num[num.size()/2];
    }
    };


Log in to reply
 

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