169. Majority Element
#array #moores_voting_algorithm
Last updated
#array #moores_voting_algorithm
Last updated
Given an array find the element that appears more than times ( assume such majority element always exists in the array )
Solve this problem in linear time using constant space
We can use the simple voting mechanism where anything different is considered a vote against.
Only the majority element will have vote in his favour.
Track the candidate
& the number of vote
i.e its occurrence.
If we encounter an element different from the candidate
decrease the vote
count
If for the current candidate
the votes
become 0
replace it
At the end only the max occurring element will remain.
No extra space is needed