2022-11-02 13:54:53 +00:00
|
|
|
export function mostUsedChar(str) {
|
|
|
|
const charMap = {};
|
|
|
|
let max = 0;
|
|
|
|
let mostUsedChar = '';
|
|
|
|
|
|
|
|
// create character map
|
|
|
|
for (let char of str) {
|
|
|
|
if (charMap[char]) {
|
|
|
|
// increment the character's value if the character existed in the map
|
|
|
|
charMap[char]++;
|
|
|
|
} else {
|
|
|
|
// Otherwise, the value of the character will be increamented by 1
|
|
|
|
charMap[char] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the most commonly used character
|
|
|
|
for (let char in charMap) {
|
|
|
|
if (charMap[char] > max) {
|
|
|
|
max = charMap[char];
|
|
|
|
mostUsedChar = char;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mostUsedChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(mostUsedChar("fdgdfgff"));
|
|
|
|
|
|
|
|
console.log(mostUsedChar("Lorem ipsum"));
|
|
|
|
|
|
|
|
console.log(mostUsedChar("adsassdasd"));
|
|
|
|
|
|
|
|
console.log(mostUsedChar("testeeeee"));
|