Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Moore Voting Algorithm
#python 3.6.9 # Find the element in array which has frequency greater than n/2 where n=length of array def isMajor(List): maj_index=0 count = 1 for i in range(len(List)): if(List[maj_index]==List[i]): count+=1 else: count -= 1 if count==0: maj_index = i count = 1 return List[maj_index] def majorElement(List,cand): count = 0 for i in range(len(List)): if List[i] == cand: count += 1 if(count > len(List)/2): return cand else: return -1 List = [1,3,3,2,2] maj_index = isMajor(List) if(majorElement(List,maj_index)!= -1): print("Major element",majorElement(List,maj_index)) else: print("No major element")
run
|
edit
|
history
|
help
0
PyPoly
Temp
single inheritance
sensi cod
Iterator-1
Finding avg value on list
Yogesh.py
shuru3
Calculator.py
Greatest Common Factor (Euclidean algorithm, aka Euclid's algorithm)