Implement the method majorityElement that returns the value appearing more than half the time.
The majority element appears more than size / 2 times. The test data always contains one.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Return the value, not its count.
- A constant-memory voting scan is possible.
Maintain a candidate and counter. Different values cancel each other until the majority candidate remains.