clueless coding // TODO: be smarter

LeetCode Majority Element: Java Solution



TL;DR Code Solution


public int majorityElement(int[] nums) {

    int vote = 1;
    int candidate = nums[0];

    for (int i = 1; i < nums.length; i++) {

        if (nums[i] == candidate) {
            vote++;
        }
        else {
            vote--;
        }

        if (vote == 0) {
            candidate = nums[i];
            vote = 1;
        }

    }

    return candidate;

}