Created
December 24, 2020 03:09
-
-
Save neuralline/2ac22ccf2fa8b7d534c84cbfff4fa19e to your computer and use it in GitHub Desktop.
Return maximum occurring character in an input string with functional JavaScript
This file contains 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
/** | |
* 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