169. Majority Element

class Solution(object):
  def majorityElement(self, nums):
  """
  :type nums: List[int]
  :rtype: int
  """
  n=len(nums)
  a=n/2
  if n>=2:
    freq={}
    for i in nums:
      if i not in freq:
        freq[i]=1
      else:
        freq[i] +=1
        if freq[i]>a:
          return i
  else:
    return nums[0]

posted @ 2018-06-26 05:56  ffeng0312  阅读(137)  评论(0)    收藏  举报