56ms Python Solution, O(1) space


  • 0
    L

    '''

    def majorityElement(self,nums):
        rep = 0
        for i in range(len(nums)):
            if rep==0 :
                candidate = nums[i]
                rep = 1
            else:
                if candidate == nums[i]:
                    rep += 1
                else:
                    rep -= 1
        return candidate
    

    '''


Log in to reply
 

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