Majority Element II
MediumP3Coding
Given an integer array nums, return every element that appears more than floor(n / 3) times, where n is the length of the array. The result may be in any order. Return an empty array if there is no such element.
Try to do it in O(n) time and O(1) extra space.
Example 1
- Input
- nums = [3,2,3]
- Output
- [3]
- Why
- 3 appears twice, which is more than floor(3/3) = 1.
Example 2
- Input
- nums = [1]
- Output
- [1]
- Why
- 1 appears once, more than floor(1/3) = 0.
Example 3
- Input
- nums = [1,2]
- Output
- [1,2]
Constraints
- 0 <= nums.length <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
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.