Majority Element
- python
- hash tables
- sorting
Problem URL:Majority Element
My Solution
Python
def majorityElement(nums: List[int]) -> int:
sorted_nums = sorted(nums)
uniques = {}
for i in sorted_nums:
if i not in uniques:
uniques[i] = 1
else:
uniques[i] += 1
uniques_keys = list(uniques.keys())
uniques_vals = list(uniques.values())
return uniques_keys[uniques_vals.index(max(uniques_vals))]
Let's Connect
Twitter •GitHub •LinkedIn