Majority Element
EasyP2Coding
Given an array nums of length n, return the element that appears more than n / 2 times (strictly more than half). Such an element always exists.
Example 1
- Input
- nums = [3,2,3]
- Output
- 3
Example 2
- Input
- nums = [2,2,1,1,1,2,2]
- Output
- 2
Example 3
- Input
- nums = [1]
- Output
- 1
Constraints
- 1 <= nums.length <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
- A majority element always exists.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.