Given an integer array nums of length n, find all values that appear more than ⌊n/3⌋ times.
Function Signature
def majority_elements(nums: list[int]) -> list[int]:
Rules
-
Return the qualifying values in ascending order, each value once.
-
Return an empty list when no value qualifies.
Constraints
-
1 <= len(nums) <= 5 * 10^4
-
-10^9 <= nums[i] <= 10^9
Examples
Input: nums = [3,2,3]
Output: [3]
Here n=3, so the threshold is ⌊3/3⌋=1. The value 3 appears 2 times, which is more than 1, while 2 appears only once.
Input: nums = [1]
Output: [1]
The threshold is ⌊1/3⌋=0, and 1 appears once, which is more than 0.
Input: nums = [9,9,4,4,4,9,1]
Output: [4,9]
Here n=7 and the threshold is 2. Both 4 and 9 appear 3 times, 1 appears once, and the two qualifying values are listed in ascending order.