Skip to main content

169. Majority Element

https://leetcode.com/problems/majority-element/

Python

from collections import Counter


class Solution:
def majorityElement(self, nums: List[int]) -> int:
if len(nums) < 2:
return nums[0] if nums else None

counts = Counter(nums)
max_counts = max(counts.items(), key=lambda count: count[1])

return max_counts[0]