Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 581 Bytes

MajorityElement.md

File metadata and controls

27 lines (25 loc) · 581 Bytes
class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sorted(nums)[len(nums)/2]

O(n) time & O(1) space |

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        count = 0
        majorElement = None
        for num in nums:
            if count == 0:
                majorElement = num
            count += 1 if majorElement == num else -1
        return majorElement
        ```