Created
April 25, 2024 13:20
-
-
Save wpjerrykwok/62324c20fcf3bd85f9f7f2163ae44ded to your computer and use it in GitHub Desktop.
LeetCode - Top Interview 150 - Array - 169. Majority Element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def majorityElement(self, nums: List[int]) -> int: | |
result = {} | |
for num in nums: | |
if num not in result.keys(): | |
result[num] = 1 | |
else: | |
result[num] += 1 | |
for k in result: | |
if result[k] > len(nums)/2: | |
return k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment