Posts E169
Post
Cancel

E169

169. Majority Element

ex_screenshot

Solution

1
2
3
4
5
6
7
class Solution:
    def majorityElement(self, nums):
        num_list = sorted(set(nums))
        
        for selec_num in num_list:
            if nums.count(selec_num) > len(nums)/2:
                return selec_num
1
solution = Solution()

Example

1
2
3
nums = [2,2,1,1,1,2,2]
ans = solution.majorityElement(nums)
print(ans)
1
2
This post is licensed under CC BY 4.0 by the author.