169. Majority Element

;  |     Back to Homepage   |     Back to Code List


class Solution {
    public int majorityElement(int[] nums) {
        int ele = 0, vote = 0;
        for (int num : nums) {
            if (ele == num && vote > 0) {
                vote += 1;
            } else if (vote == 0) {
                ele = num;
                vote = 1;
            } else {
                vote -= 1;
            }
        }
        return ele;
    }
}