56ms Python Solution


  • -1
    Y
    def majorityElement(self, nums):
        if len(nums)==1 or len(nums)==2:
            return nums[0]
        else:
            l=sorted(nums)
            x=l[:len(nums)/2+1]
            y=l[len(nums)/2+1:]
            a=set(x)
            b=set(y)
            if len(a)==1:
               return x[0]
            else:
               return y[0]

  • 4
    A
    def majorityElement(nums):
            return sorted(nums)[len(nums)/2]

Log in to reply
 

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