How to find the highest occuring word in an array of words [JavaScript]

Greetings everyone, presently on the iterators section of the javascript course. I tried completed the quiz and tried taking one of the challenges to further my skills, got stuck at trying to check the highest occurring word in an array

take for example this array, how will you check the highest occuring word?

const wordsArray = ['Hello', 'sir', 'Hello', 'madam', 'Hello', 'students', 'Hello', 'everyone' ]

Obviously it’s Hello. but in an array of a thousand words how can one easily know which word has the highest occurrence?

Please I will appreciate a beginner level breakdown, and explanation. Thank you!

You might find the number of occurrences of each word (and have that information stored as an object or a 2-dimensional array [meaning an array of arrays] ).
And then maybe find the word that has the maximum number of occurrences.

I don’t want to give away the details of the code yet, because it is a challenge on here, and figuring out the code is a useful learning experience.

1 Like

This hint made me work through this thanks.

Here is my sample code

//ARRAY OF NUMBERS const nums = [1,2,3,4,5,3,8,3,5,3,8,8,8,8,8,8,8,8,9,10,3,2,1,6,3] //ARRAY TO CHECK HIGHEST OCCURING NUMBER AND HOW MANY TIMES IT OCCURS function highestIn(arr){ const obj = {}; for (let x of arr){ if(obj[x]){ obj[x] += 1; }else{ obj[x] = 1 } } const newArr = [] for (let x in obj) { newArr.push([x,obj[x]]) } newArr.sort((a,b)=>b[1]-a[1]) return `The highest number is ${newArr[0][0]} (${newArr[0][1]} times)`; } console.log(highestIn(nums)) //LOGS: "The highest number is 8 (9 times)"