I'm trying to create a counter that says which character appears the most in a string.
function maxChar(str: string) {
const strObj = {}
let maxCount = 0
let maxChar = ""
for (let char of str) {
strObj[char] = strObj[char] + 1 || 1
}
for (let key in strObj) {
if (strObj[key] > maxCount) {
maxCount = strObj[key]
maxChar = key
}
}
return maxChar
}
However, Typescript underscores strObj[char] and strObj[key], giving error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'
Apparently strObj = {} needs a type assigned, but I have no idea how to assign a type to an empty object.