Simple C solution 8ms


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

Log in to reply
 

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