Problem:
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
Answer:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = {}
for i in nums:
if i not in result:
result[i] = 0
result[i] += 1
big_most = nums[0]
big_most_count = 0
for key,value in result.items():
if value >= big_most_count:
big_most = key
big_most_count = value
return big_most