Created
August 16, 2019 10:44
-
-
Save priyankvex/91873e05f27c0f9fa5527d719c1bb58d to your computer and use it in GitHub Desktop.
Find the 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(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