You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This solution uses the Boyer-Moore Voting Algorithm to find the majority element (an element that appears more than n/2 times) in linear time with constant space. The algorithm maintains a count (i) and a candidate (j). It iterates through the array, updating the candidate whenever the count reaches zero. If the current element matches the candidate, the count increases; otherwise, it decreases. At the end of the iteration, the candidate is guaranteed to be the majority element.
Time Complexity: O(n)
Space Complexity: O(1)
0 commit comments