-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Moore Voting Algorithm #2830
Labels
enhancement
New feature or request
Comments
feel free to raise a pr and link it here |
sure |
Hey , Can you assign this to me |
@realstealthninja I have implemented the algorithm and have raised a PR . Looking forward to your feedback. Thank you for the opportunity to contribute! |
assign this to me |
/assign |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Detailed description
Detailed description:
Problem Statement:
The Moore Voting Algorithm is an efficient way to find the majority element in an array. A majority element is defined as an element that appears more than n/2 times in the array, where n is the length of the array. The algorithm works in two phases:
Candidate Selection: Traverse the array and choose a candidate for the majority element by maintaining a count. When the count reaches zero, a new candidate is selected.
Verification: Verify that the candidate is indeed the majority element by counting its occurrences in a second pass through the array.
The algorithm has a time complexity of O(n) and a space complexity of O(1).
Context
This algorithm is important in scenarios where the majority element needs to be identified efficiently, such as in voting systems or stream processing applications. The typical brute-force or sorting approaches have higher time complexities, whereas Moore Voting Algorithm is optimal for this type of problem.
This change benefits users who are working on algorithms that involve large datasets, providing them with a more efficient solution for finding a dominant or majority element in a list or array.
Possible implementation
Possible implementation:
The Moore Voting Algorithm can be implemented with the following steps:
Initialize a variable candidate to store the current candidate and a variable count to track the candidate’s occurrences.
Traverse the array, adjusting the candidate and count.
Verify by making a second pass through the array to check if the selected candidate appears more than n / 2 times.
Test Case Example:
Input:
[3, 3, 4, 2, 4, 4, 2, 4, 4]
Expected Output:
Majority Element: 4
Additional information
No response
The text was updated successfully, but these errors were encountered: