Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://leetcode.com/problems/majority-element/submissions/
- class Solution:
- def majorityElement(self, nums: List[int]) -> int:
- major=nums[0]
- count=1
- n=len(nums)
- for i in range(1,n):
- if nums[i]==major:
- count+=1
- else:
- if count>1:
- count=count-1
- else:
- count=1
- major=nums[i]
- check=0
- for i in range(n):
- if nums[i]==major:
- check+=1
- if check>n//2:
- return major
Add Comment
Please, Sign In to add comment