Added new algoritm

This commit is contained in:
Carlos Carvalho
2020-10-06 23:50:42 -03:00
parent 7dfcfef794
commit c7517885a2
2 changed files with 41 additions and 0 deletions

29
String/MaxCharacter.js Normal file
View File

@ -0,0 +1,29 @@
/*
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')
}
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
}
let maxCharacter = null
let maxCount = 0
Object.keys(occurrences).forEach(char => {
if (occurrences[char] > maxCount) {
maxCount = occurrences[char]
maxCharacter = char
}
})
return maxCharacter
}
export { maxCharacter }