My python solution (96ms)


  • 0
    Y

    This is an incomplete hash table solution, cost 96ms. There is no need to complete the table because there is only one majority element in nums

    class Solution:
            # @param {integer[]} nums
            # @return {integer}
            def majorityElement(self, nums):
                count = {}
                for i in nums:
                    if i not in count:
                        count[i] = 0
                    count[i] += 1
                    if count[i] > len(nums)/2:
                        return i

  • 5
    B

    Well... one liner:

    return sorted(nums)[len(nums) / 2]
    

    runs in 66ms :)


  • 0
    D

    What if there are two Majority Elements in it..

    Nevermind, i was stupid! There can't be more than one..


Log in to reply
 

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