Skip to content

Commit 2806253

Browse files
authored
merge: Upgrade max char (TheAlgorithms#983)
* feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544d. * remove: yarn lock * chore: fix grammer * feat: used replace method & added test case * feat: remove revert * chore: added new line * feat: added filter for alphabets only * resolve: added empty validation improved ignore pattern * chore: reverted to for of loop
1 parent e447b55 commit 2806253

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

String/MaxCharacter.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
/*
2-
Given a string of characters, return the character that appears the most often.
3-
Example: input = "Hello World!" return "l"
4-
*/
5-
const maxCharacter = (value) => {
6-
if (typeof value !== 'string') {
7-
throw new TypeError('The param should be a string')
8-
} else if (!value) {
9-
throw new Error('The param should be a valid string')
1+
/**
2+
* @function maxCharacter
3+
* @example - Given a string of characters, return the character that appears the most often. Example: input = "Hello World!" return "l"
4+
* @param {string} str
5+
* @param {RegExp} ignorePattern - ignore the char in str that is not required
6+
* @returns {string} - char
7+
*/
8+
const maxCharacter = (str, ignorePattern) => { // initially it's count only alphabets
9+
if (typeof str !== 'string') {
10+
throw new TypeError('Argument should be a string')
11+
} else if (!str) {
12+
throw new Error('The param should be a nonempty string')
1013
}
1114

12-
const occurrences = {}
13-
for (let i = 0; i < value.length; i++) {
14-
const char = value[i]
15-
if (/\s/.test(char)) continue
16-
occurrences[char] = occurrences[char] + 1 || 1
15+
// store all char in occurrence map
16+
const occurrenceMap = new Map()
17+
18+
for (const char of str) {
19+
if (!ignorePattern?.test(char)) {
20+
occurrenceMap.set(char, occurrenceMap.get(char) + 1 || 1)
21+
}
1722
}
18-
let maxCharacter = null
19-
let maxCount = 0
20-
Object.keys(occurrences).forEach(char => {
21-
if (occurrences[char] > maxCount) {
22-
maxCount = occurrences[char]
23-
maxCharacter = char
23+
24+
// find the max char from the occurrence map
25+
let max = { char: '', occur: -Infinity }
26+
27+
for (const [char, occur] of occurrenceMap) {
28+
if (occur > max.occur) {
29+
max = { char, occur }
2430
}
25-
})
26-
return maxCharacter
31+
}
32+
33+
return max.char
2734
}
2835

29-
export { maxCharacter }
36+
export default maxCharacter

String/test/MaxCharacter.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { maxCharacter } from '../MaxCharacter'
1+
import maxCharacter from '../MaxCharacter'
22

33
describe('Testing the maxCharacter function', () => {
44
it('Expect throw with wrong arg', () => {
55
expect(() => maxCharacter(123)).toThrow()
6+
expect(() => maxCharacter('')).toThrow()
67
})
8+
79
it('Check the max character in string', () => {
810
const theString = 'I can\'t do that'
9-
const maxChar = maxCharacter(theString)
11+
const maxCharInAllCount = maxCharacter(theString)
12+
const maxChar = maxCharacter(theString, /\s/)
13+
14+
expect(maxCharInAllCount).toBe(' ')
1015
expect(maxChar).toBe('t')
16+
17+
expect(maxCharacter('!!!Hello, World!!!', /[a-z]/)).toBe('!')
18+
19+
expect(maxCharacter('!!!Hello, World!!!', /[^a-z]/i)).toBe('l')
1120
})
1221
})

0 commit comments

Comments
 (0)