Majority Element
Repeating Number
Last updated
Was this helpful?
Repeating Number
Last updated
Was this helpful?
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example:
Moore's Voting Algorithm: Moore's Voting Algorithm is an algorithm for finding the majority in a sequence of elements using linear time and constant space.
For this problem, we will:
1) Find the candidate with (this is where we use Moore's to find a potential candidate for majority element)
However, just because its the candidate does not mean it occurs more than times. This is why we have to verify it
2) Iterate back through the array to verify this candidate appears more than times.
Moore's Voting Algorithm is important for this problem as it helps us potentially find the majority element. We then have to verify if this element is in fact the majority element
We intially have to set the first element as the majority element to start.
Time:
Space: