Python 45ms solution using set()


  • 0
    Y
    class Solution(object):
        def majorityElement(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            half = int(len(nums)/2)
            nums_to_set = set(nums)
            for num in nums_to_set:
                if nums.count(num) > half:
                    return num
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.