My simple java solution


  • 0
    public class Solution {
        public int majorityElement(int[] nums) {
            int count = 1, majorEle = nums[0];
            for(int i = 1; i < nums.length; i++){
                if(nums[i] == majorEle)
                    count ++;
                else if(count > 0)
                    count --;
                else{
                    majorEle = nums[i];
                    count = 1;
                }
                    
            }
            return majorEle;
        }
    }
    

Log in to reply
 

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