java solution using crash offset method


  • 0
    M

    public int majorityElement(int[] nums) {
    int maje = nums[0];
    int count = 1;
    for(int i = 1; i < nums.length; i++) {
    if(nums[i] == maje) {
    count++;
    } else {
    if(count == 0) {
    maje = nums[i];
    count++;
    } else {
    count--;
    }
    }
    }
    return maje;
    }


Log in to reply
 

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