Skip to interview prep
Apexed

21. Majority Element

EasySelection

Return the element that appears more than ⌊n / 2⌋ times. It always exists. Try O(1) extra space.

Example 1

Input: nums = [2, 2, 1, 1, 1, 2, 2]
Output: 2

Constraints

  • 1 ≤ nums.length ≤ 5 × 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 3 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Majority Element — coding interview practice — Apexed