-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added Majority Voting Algorithm #9866
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5b3a3e9
Create MajorityVoteAlgorithm.py
sarvjeetdev 6495340
Update and rename MajorityVoteAlgorithm.py to majorityvotealgorithm.py
sarvjeetdev b881325
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e3580d4
Update and rename majorityvotealgorithm.py to majority_vote_algorithm.py
sarvjeetdev 2398221
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba13c2a
Update majority_vote_algorithm.py
sarvjeetdev 83413d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2bcd208
Update majority_vote_algorithm.py
sarvjeetdev 88efe5b
Update majority_vote_algorithm.py
sarvjeetdev b0608a0
Update other/majority_vote_algorithm.py
sarvjeetdev 6ff610a
renaming variables majority_vote_algorithm.py
sarvjeetdev 288608e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cf8a2e3
Update majority_vote_algorithm.py
sarvjeetdev 7cf0e5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fbd6082
Update majority_vote_algorithm.py
sarvjeetdev 21f5635
Update majority_vote_algorithm.py
sarvjeetdev 57e64ce
Update majority_vote_algorithm.py
sarvjeetdev 23ea6fe
Update majority_vote_algorithm.py
sarvjeetdev 5131a19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5e398b2
Update other/majority_vote_algorithm.py
sarvjeetdev 9d2e3dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 00c65aa
Update other/majority_vote_algorithm.py
sarvjeetdev a263f7a
adding more testcases majority_vote_algorithm.py
sarvjeetdev 63d60e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6756885
Update majority_vote_algorithm.py
sarvjeetdev a215f1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c0a3f88
Update majority_vote_algorithm.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
''' | ||
This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like 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 | ||
|
||
''' | ||
import collections | ||
|
||
def majorityElement(self, nums: List[int]) -> List[int]: | ||
ctr = collections.Counter() | ||
for n in nums: | ||
ctr[n] += 1 | ||
if len(ctr) == k: | ||
ctr -= collections.Counter(set(ctr)) | ||
ctr = collections.Counter(n for n in nums if n in ctr) | ||
return [n for n in ctr if ctr[n] > len(nums)/k] | ||
|
||
|
||
nums = [1,2,2,3,1,3,2] | ||
k = 3 | ||
print(majorityElement(nums,k)) | ||
|
||
''' | ||
Output: [2] | ||
''' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.