Count frequency in array by entering a specific length

For example :
We have an array of few reapeted words.
I managed to count the most “popular word” in the array but cannot understand how to make it print the most popular word with specific length .

const arr = ['a','hello,'a','hello','hello'] 

Trying to get to this result :
if ill searching for length of 5. I wanna print " hello reapeted 3 times"
If ill searching for length of 1 i wanna print " a reapeted 2 times" …

What code have you tried so far?

var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
var maxF = 1;
var m = 0;
var word;
for (var i=0; i<arr1.length; i++)
        for (var j=i; j<arr1.length; j++)
                if (arr1[i] == arr1[j])
                if (maxF<m)
                  word = arr1[i];
console.log(`${word} - ${maxF} times`);

Will output the string whos most reapted. I wanna add an length parameter to search thro it.
like it wouldnt give me automatically the most reapeted word… i will have to pass an specific length . And it will return the most reapeted word with that length .

What if two strings are repeated the same number of times? What would you want to display? For example, if the letters a and b both were repeated 5 times and you passed 5 as an argument to a function you create to display the information, what should be displayed?

if you already have a function to print the most popular word, you could just use that function on a filtered list.

const mostPopular = (arr) => {
//here's the function that you already have, it prints the most frequent element

const mostPopularLengthN = (arr,n) => {
    let filteredArr = arr.filter(el=>el.length===n);
    return mostPopular(filteredArr)

you’ll still have to handle if two strings are the same length and repeated the same amount of times, but that’s part of your original function.

1 Like

To be honest , in “real life” it would be the first which meating inside the array iteration . but for now… both ‘a’ and ‘b’ .
Because in next … i gonna try it on readFile of nodejs then i cant know from beginning if ther is few words same length… it will be an random reading of file which i will use split(" ") to make it an array and then use the function which getting the length parameter. Hope i was clear enough.