169. Majority Element

Easy

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array.

😇 Solution

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        temp = {}
        for n in nums:
            if n in temp:
                temp[n] += 1
            else:
                temp[n] = 1
        majority = int(len(nums)/2)
        for n,count in temp.items():
            if count > majority:
                return n

Last updated

Was this helpful?