Javascript String Count letter

Description

Javascript String Count letter

str = "Lorem ipsum dolor sit amet";

function charFreq(s) {
    var i, j;/* ww w.  j  a  v  a2 s .c om*/
    var a = new Array();

    for (j = 0; j < s.length; j++) {
        for (i = 0; i < a.length; i++) {
            if (a[i][0] == s[j]) {
                a[i][1]++;
                break;
            }
        }

        if (i == a.length) {

            a[i] = [s[j], 1];
        }
    }

    return a;
}
var all_words = str.split(' ');
var all_freq = all_words.map(charFreq);
var max_freq = 0;
var max_word;
for (var i = 0; i < all_freq.length; i++) {
    for (j = 0; j < all_freq[i].length; j++) {
        if (all_freq[i][j][1] > max_freq) {
            max_freq = all_freq[i][j][1];
            max_word = i;
        }
    }
}
console.log(all_words[max_word]);



PreviousNext

Related