Majority Element

Problem Id: 169 Difficulty: Easy


Intuition

Solution


class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        counts = {}
        for n in nums:
            if n in counts:
                counts[n] += 1
            else:
                counts[n] = 1

        max_count = 0
        max_num = None
        for n, c in counts.items():
            if c > max_count:
                max_count = c
                max_num = n

        return max_num