dsa · medium
Majority Element II
Return every value in nums that appears **more than** floor(n / 3) times, where n is len(nums). There are at most two such values. Return them in any order. The answer may be empty.
Arguments
nums— array in which you look for values appearing more than n/3 times
Example
nums = [3,2,3]: n = 3, floor(n/3) = 1. 3 appears twice → [3].
nums = [1,2]: n = 2, floor(n/3) = 0. Both appear once → [1,2].
nums = [1,2,3]: each appears once, which is not greater than 1 → [].
Constraints
1 <= nums.length <= 5 * 10^4 -10^9 <= nums[i] <= 10^9 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [3, 2, 3] Expected: [3]
Example 2
Input: [1] Expected: [1]
Example 3
Input: [1, 2] Expected: [1,2]