Majority Element II

nums = [99,2,99,2,99,3,3]
nums = [1, 2, 1, 2, 1, 3, 3]
def majorityNumber(self, nums):    candidate1=None    candidate2=None    vote1=0    vote2=0    one_3=len(nums)//3    for i in nums:      if(candidate1==i):        vote1+=1      elif(candidate2==i):        vote2+=1      elif(vote1==0):        candidate1=i        vote1=1      elif(vote2==0):        candidate2=i        vote2=1      else:        vote1-=1        vote2-=1       #end of for   count_of_cand1=0   count_of_cand2=0   for i in nums:     if(i==candidate1):       count_of_cand1+=1     elif(i==candidate2):       count_of_cand2+=1   print(candidate1,candidate2)   return [candidate1,candidate2][count_of_cand2>one_3]



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.