Skip to content

Instantly share code, notes, and snippets.

@neuralline
Created December 24, 2020 03:09
Show Gist options
  • Save neuralline/2ac22ccf2fa8b7d534c84cbfff4fa19e to your computer and use it in GitHub Desktop.
Save neuralline/2ac22ccf2fa8b7d534c84cbfff4fa19e to your computer and use it in GitHub Desktop.
Return maximum occurring character in an input string with functional JavaScript
/**
* Javascript Algorithms.
* Functional javascript using pure, recursive, compositing and ....
* @Licence Don't use this code for anything ever! :) but if you do, give credit where credit is due, MIT.
* @author Darik.
* @GitHub neuralline
*
*/
/**
* MAXIMUM CHARACTER FUNCTION
* @param {string} str please provide a string
* @returns {[string, number]} ['max character', 'number of occurrence']
* @description Return maximum occurring character in an input string
*
*
*/
const maximumCharacter = str => {
const stringToObj = {}
let max = ['', 0]
const result = [...str.toLowerCase().trim()].map(char => {
if (char.trim() === '') return
stringToObj[char] = stringToObj[char] + 1 || 1
max[1] < stringToObj[char] && (max = [char, stringToObj[char]])
})
return max
}
module.exports = maximumCharacter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment