Solution feedback

Hello, I came across a problem that requires to find the character that occurs the most in a string. This is my solution for it. I would like to receive some feedback on it and it would be great if you guys share your solutions too.

let x = 'tili dili trali vali'
function test(str) {
    str = str.split(''); // split the string into an array of words
    let occurrences = []; // create an array to store the occurrences of each word
    for (let word of str) { // loop through the array, pushing the number of occurences into the occurences array at respective indexes 
       occurrences.push(str.filter(a => a === word).length)
    };
    let idx = occurrences.indexOf(Math.max(...occurrences)); // find the index of the largest number in the occurences array
    return str[idx] // return the word from str array at idx
}

console.log(test(x))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.