55. Majority element

MediumProgramAcademy Pass20 points

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

  1. InputmajorityElement [3, 2, 3]Output3
  2. InputmajorityElement [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.

Solution.lean
Loading editor…

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