Skip to content
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

Added Majority Voting Algorithm #9866

Merged
merged 27 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5b3a3e9
Create MajorityVoteAlgorithm.py
sarvjeetdev Oct 5, 2023
6495340
Update and rename MajorityVoteAlgorithm.py to majorityvotealgorithm.py
sarvjeetdev Oct 5, 2023
b881325
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
e3580d4
Update and rename majorityvotealgorithm.py to majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
2398221
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
ba13c2a
Update majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
83413d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
2bcd208
Update majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
88efe5b
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
b0608a0
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
6ff610a
renaming variables majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
288608e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
cf8a2e3
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
7cf0e5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
fbd6082
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
21f5635
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
57e64ce
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
23ea6fe
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
5131a19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
5e398b2
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
9d2e3dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
00c65aa
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
a263f7a
adding more testcases majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
63d60e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
6756885
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
a215f1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
c0a3f88
Update majority_vote_algorithm.py
cclauss Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions other/majority_vote_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like this:
Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times.
We have to solve in O(n) time and O(1) Space.
URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
"""
from collections import Counter


def majority_vote(votes: list[int], votes_needed_to_win: int) -> list[int]:
"""
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 3)
[2]
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 2)
[]
>>> majority_vote([1, 2, 2, 3, 1, 3, 2], 4)
[1, 2, 3]
"""
majority_candidate_counter: Counter[int] = Counter()
for vote in votes:
majority_candidate_counter[vote] += 1
if len(majority_candidate_counter) == votes_needed_to_win:
majority_candidate_counter -= Counter(set(majority_candidate_counter))
majority_candidate_counter = Counter(
vote for vote in votes if vote in majority_candidate_counter
)
return [
vote
for vote in majority_candidate_counter
if majority_candidate_counter[vote] > len(votes) / votes_needed_to_win
]


if __name__ == "__main__":
import doctest

doctest.testmod()