Given an array nums of size n, return the majority element.
The majority element is the element that appears strictly more than floor(n / 2) times. You may assume that the majority element always exists in the array.
Solve the problem in linear time and in O(1) space.
7 2 2 1 1 1 2 2
2
2 appears 4 times, which is > 7 / 2 = 3. Majority element is 2.
Boyer-Moore voting algorithm: maintain candidate and count = 0. For each x in nums: if count == 0, candidate = x. count += (x == candidate) ? 1 : -1. Return candidate.