An array contains a majority element: a value that appears strictly more than n/2 times. It is guaranteed that such an element exists. Output it.
Line 1: an integer n.
Line 2: n space-separated integers.
One line: the majority element.
Example 1
Input
7 3 3 4 2 3 3 3
Expected
3
Explanation
3 appears five times out of seven, more than half, so it is the majority element.
Example 2
Input
1 9
Expected
9
Explanation
A single element is trivially the majority.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →