Skip to content

Instantly share code, notes, and snippets.

@priyankvex
Created August 16, 2019 10:44
Show Gist options
  • Save priyankvex/91873e05f27c0f9fa5527d719c1bb58d to your computer and use it in GitHub Desktop.
Save priyankvex/91873e05f27c0f9fa5527d719c1bb58d to your computer and use it in GitHub Desktop.
Find the majority element
class Solution(object):
def solution(self, a):
count = 0
ele = None
for n in a:
if ele is None:
count = 1
ele = n
else:
if ele == n:
count += 1
else:
if count != 0:
count -= 1
if count == 0:
ele = n
count = 1
return ele
if __name__ == "__main__":
a = [1, 1, 2, 2, 4]
ans = Solution().solution(a)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment