All problems
Majority Element II
Problem
Return all values that appear more than floor(n / 3) times in nums, sorted in ascending order.
Count occurrences with a dictionary and keep every value whose count exceeds n // 3. Return an empty list for empty input.
Hint
At most two values can appear more than n/3 times, but counting is simpler than the Boyer-Moore variant.
Test cases
case 1
in: [[3,2,3]]
expected: [3]
case 2
in: [[1,1,1,3,3,2,2,2]]
expected: [1,2]
case 3
in: [[1]]
expected: [1]
case 4
in: [[]]
expected: []
case 5
in: [[1,2]]
expected: [1,2]
Starter codePython
def majority_element_ii(nums):
# Your code here
pass5 test cases · run your code against them in the editor