-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add majority element #1136
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
Closed
Closed
add majority element #1136
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d082d37
add majority element
shivamp1998 da6ad7c
majority element review fixes
shivamp1998 30a9960
Review fixes
shivamp1998 b29cf76
Merge branch 'TheAlgorithms:master' into master
shivamp1998 1847096
Apply suggestions from code review
shivamp1998 4e2d2dd
Apply suggestions from code review
shivamp1998 cde547a
Merge branch 'TheAlgorithms:master' into master
shivamp1998 3e827c0
Merge branch 'TheAlgorithms:master' into master
shivamp1998 c48c32a
majority element fixes
shivamp1998 fad5f6d
merge fixes
shivamp1998 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
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
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
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
Oops, something went wrong.
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,16 @@ | ||
import caesarsCipher from '../CaesarsCipher' | ||
|
||
describe('Testing the caesarsCipher function', () => { | ||
it('Test - 1, Testing for invalid types', () => { | ||
expect(() => caesarsCipher(false, 3)).toThrow() | ||
expect(() => caesarsCipher('false', -1)).toThrow() | ||
expect(() => caesarsCipher('true', null)).toThrow() | ||
}) | ||
|
||
it('Test - 2, Testing for valid string and rotation', () => { | ||
expect(caesarsCipher('middle-Outz', 2)).toBe('okffng-Qwvb') | ||
expect(caesarsCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe('defghijklmnopqrstuvwxyzabc') | ||
expect(caesarsCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe('Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj') | ||
expect(caesarsCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD') | ||
}) | ||
}) |
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,46 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment needs to be on the |
||
* [Majority Element](https://www.geeksforgeeks.org/majority-element/) A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element) | ||
* @complexity: O(n) (on average ) | ||
* @complexity: O(n) (worst case) | ||
* @flow | ||
*/ | ||
|
||
function findCandidate (array) { | ||
let indexMajority = 0 | ||
let count = 1 | ||
const size = array.length | ||
for (let i = 1; i < size; i++) { | ||
if (array[indexMajority] === array[i]) { | ||
count++ | ||
} else { | ||
count-- | ||
if (count === 0) { | ||
indexMajority = i | ||
count = 1 | ||
} | ||
} | ||
} | ||
return array[indexMajority] | ||
} | ||
|
||
// verifies if candidate occurs more than size/2 times in an array | ||
function isMajorityElement (array, candidate) { | ||
let count = 0 | ||
const size = array.length | ||
for (let i = 0; i < size; i++) { | ||
if (array[i] === candidate) { | ||
if (++count > size / 2) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
function findMajorityElement (array) { | ||
// finds the candidate for majority | ||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
shivamp1998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const cand = findCandidate(array) | ||
return isMajorityElement(array, cand) ? cand : -1 | ||
} | ||
|
||
export { findMajorityElement } |
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,14 @@ | ||
import { findMajorityElement } from '../MajorityElement' | ||
|
||
describe('Majority element cases', () => { | ||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please inline your arrays for all test cases. |
||
it('no majority element in array test', () => { | ||
expect(findMajorityElement([1, 2, 3, 4, 5])).toEqual(-1) | ||
}) | ||
it('One majority element present', () => { | ||
expect(findMajorityElement([3, 3, 4, 2, 4, 4, 2, 4, 4])).toEqual(4) | ||
}) | ||
it('Majority element when array elements are exactly equal to n/2 ', () => { | ||
const Array = [3, 4, 2, 2, 2, 2, 8, 4] | ||
expect(findMajorityElement(Array)).toEqual(-1) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.