169. Majority Element
[Easy] “Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times."
Note: old solution, should review (TODO:
)
Python3:
class Solution:
def majorityElement(self, nums: List[int]) -> int:
uniq_nums = list(set(nums))
max_count_and_num = [0,0]
target = len(nums) / 2
for n in uniq_nums:
if nums.count(n) > target:
return n
if nums.count(n) > max_count_and_num[0]:
max_count_and_num[0] = nums.count(n)
max_count_and_num[1] = n
return max_count_and_num[1]