-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Upgrade max char #983
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
raklaptudirm
merged 43 commits into
TheAlgorithms:master
from
fahimfaisaal:upgrade-MaxChar
May 2, 2022
Merged
Upgrade max char #983
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
72def41
feat: improved memoize function
fahimfaisaal 8548ef1
docs: modified documentation
fahimfaisaal ce9d3b9
style: format with standard
fahimfaisaal 1e2fb53
docs: modified stringify doc
fahimfaisaal 2a5c28b
refactor: remove two repetition implementation
fahimfaisaal c582f58
feat: added validation, test codes
fahimfaisaal cdff0e3
chore: remove useless words
fahimfaisaal 5727b64
feat: added types for jest
fahimfaisaal 025a8b8
Merge branch 'upgrade-abs' of https://github.com/fahimfaisaal/Javascr…
fahimfaisaal 9efda24
chore: added link box
fahimfaisaal 98ffd1d
feat: added new validation test casses & methods
fahimfaisaal b8d6e68
style: formated with standard
fahimfaisaal b7c961c
feat: added parse method & test cases
fahimfaisaal 6b7b715
docs: added js docs
fahimfaisaal 269a3f4
chore: added default import export
fahimfaisaal 5cedaa9
feat: imporved algorithm via replace method
fahimfaisaal d00a7bf
test: added two test cases
fahimfaisaal bbcce39
feat: added jest type for suggestions
fahimfaisaal 9be30a8
feat: added `reduceRight` & `trim` method
fahimfaisaal 489544d
chore: added helper variable
fahimfaisaal 68cb5ad
feat: added new rotation option
fahimfaisaal addf721
Revert "chore: added helper variable"
fahimfaisaal 07a2b17
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 8c297ae
Merge branch 'upgrade-CaesarsCipher'
fahimfaisaal 605cfb7
Merge branch 'upgrade-reverseWord'
fahimfaisaal 6636af2
Merge branch 'upgrade-packageJson'
fahimfaisaal 3905c5d
Merge branch 'upgrade-LRU'
fahimfaisaal 42b13b8
Merge branch 'upgrade-ciphers'
fahimfaisaal 64981b7
Merge branch 'upgrade-memoize'
fahimfaisaal e1c6666
Merge branch 'upgrade-abs'
fahimfaisaal e783547
remove: yarn lock
fahimfaisaal 399e353
chore: fix grammer
fahimfaisaal 189e492
Merge branch 'master' of github.com:fahimfaisaal/Javascript
fahimfaisaal 4887aff
feat: used replace method & added test case
fahimfaisaal a946220
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 48329bf
feat: remove revert
fahimfaisaal 08955a7
chore: added new line
fahimfaisaal a613aee
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 05ce4b8
Merge branch 'upgrade-XORCipher'
fahimfaisaal 91f6c78
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal c61506a
feat: added filter for alphabets only
fahimfaisaal 41becaa
resolve: added empty validation improved ignore pattern
fahimfaisaal 7b812b5
chore: reverted to for of loop
fahimfaisaal 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 |
---|---|---|
@@ -1,29 +1,36 @@ | ||
/* | ||
Given a string of characters, return the character that appears the most often. | ||
Example: input = "Hello World!" return "l" | ||
*/ | ||
const maxCharacter = (value) => { | ||
if (typeof value !== 'string') { | ||
throw new TypeError('The param should be a string') | ||
} else if (!value) { | ||
throw new Error('The param should be a valid string') | ||
/** | ||
* @function maxCharacter | ||
* @example - Given a string of characters, return the character that appears the most often. Example: input = "Hello World!" return "l" | ||
* @param {string} str | ||
* @param {RegExp} ignorePattern - ignore the char in str that is not required | ||
* @returns {string} - char | ||
*/ | ||
const maxCharacter = (str, ignorePattern) => { // initially it's count only alphabets | ||
if (typeof str !== 'string') { | ||
throw new TypeError('Argument should be a string') | ||
} else if (!str) { | ||
throw new Error('The param should be a nonempty string') | ||
} | ||
|
||
const occurrences = {} | ||
for (let i = 0; i < value.length; i++) { | ||
const char = value[i] | ||
if (/\s/.test(char)) continue | ||
occurrences[char] = occurrences[char] + 1 || 1 | ||
// store all char in occurrence map | ||
const occurrenceMap = new Map() | ||
fahimfaisaal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (const char of str) { | ||
if (!ignorePattern?.test(char)) { | ||
occurrenceMap.set(char, occurrenceMap.get(char) + 1 || 1) | ||
} | ||
} | ||
let maxCharacter = null | ||
let maxCount = 0 | ||
Object.keys(occurrences).forEach(char => { | ||
if (occurrences[char] > maxCount) { | ||
maxCount = occurrences[char] | ||
maxCharacter = char | ||
|
||
// find the max char from the occurrence map | ||
let max = { char: '', occur: -Infinity } | ||
fahimfaisaal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (const [char, occur] of occurrenceMap) { | ||
if (occur > max.occur) { | ||
max = { char, occur } | ||
} | ||
}) | ||
return maxCharacter | ||
} | ||
|
||
return max.char | ||
} | ||
|
||
export { maxCharacter } | ||
export default maxCharacter |
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 |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import { maxCharacter } from '../MaxCharacter' | ||
import maxCharacter from '../MaxCharacter' | ||
|
||
describe('Testing the maxCharacter function', () => { | ||
it('Expect throw with wrong arg', () => { | ||
expect(() => maxCharacter(123)).toThrow() | ||
expect(() => maxCharacter('')).toThrow() | ||
}) | ||
|
||
it('Check the max character in string', () => { | ||
const theString = 'I can\'t do that' | ||
const maxChar = maxCharacter(theString) | ||
const maxCharInAllCount = maxCharacter(theString) | ||
const maxChar = maxCharacter(theString, /\s/) | ||
|
||
expect(maxCharInAllCount).toBe(' ') | ||
expect(maxChar).toBe('t') | ||
|
||
expect(maxCharacter('!!!Hello, World!!!', /[a-z]/)).toBe('!') | ||
|
||
expect(maxCharacter('!!!Hello, World!!!', /[^a-z]/i)).toBe('l') | ||
}) | ||
}) |
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.