55. Majority element
More than half of the elements of xs are the same value. Write majorityElement xs, that value. The list is never empty.
The tests include a list of a hundred thousand numbers, so counting every candidate separately is too slow.
Examples
- Input
majorityElement [3, 2, 3]Output3 - Input
majorityElement [2, 2, 1, 1, 1, 2, 2]Output2
Submitting also runs 5 hidden tests.
Running and submitting solutions, the hints and the editorial come with the Academy Pass. The daily problem and one Medium problem a week are open to every account.
Loading editor…
Type
\to in the editor, or click:Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.
ReadyLn 1, Col 1Lean 4
Draft saved in this browser