C greedy solution


  • 0
    Y
    int majorityElement(int* nums, int numsSize) {
        int count = 0;
        int res;
        for (int i = 0; i < numsSize; ++i) {
            if (count == 0) {
                res = nums[i];
                count++;
            } else {
                if (res == nums[i])
                    count++;
                else
                    count--;
            }
        }
        return res;
    }
    

Log in to reply
 

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