added majority element

pull/310/head
Sankalp Sharma 2021-05-17 10:58:36 +05:30
parent eb0628b0e7
commit f1ace9bb8b
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
"""
Algorithm Type: Array Traversal
Time Complexity: O(n)
"""
numbers = [2, 2, 5, 6, 2, 6, 2, 10, 2]
def majority_element(numbers):
m = float("inf")
cnt = 0
for num in numbers:
if cnt == 0:
m = num
cnt += 1
elif m == num:
cnt += 1
else:
cnt -= 1
# to verify the most frequent number is greater than length of aaray//2
recheck_cnt = 0
for num in numbers:
if num == m:
recheck_cnt += 1
return m if recheck_cnt > len(numbers)//2 else None
if __name__ == "__main__":
print(majority_element(numbers))