C++ O(nlogn) solution


  • 0
    W

    As the array is already sorted,we could choose the number which its indice is n/2 as the result.

        int majorityElement(vector<int>& nums)
        {
            int len=nums.size();
            sort(nums.begin(),nums.end());
            return nums[len/2];
        }
    

Log in to reply
 

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