Desafio 9 Feito
This commit is contained in:
parent
dc3cfbeb10
commit
cb1011783f
@ -1,4 +1,34 @@
|
||||
export function mostUsedChar(text) {
|
||||
// implementar logica aqui
|
||||
return ""
|
||||
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"));
|
Loading…
Reference in New Issue
Block a user