I was having fun on solving one of the HackerRank simple challenges- Birthday Cake Candles. DescriptionColleen is turning n years old! Therefore, she has n candles of various heights on her cake, and candle i has heighti. Because the taller candles tower over the shorter ones, Colleen can only blow out the tallest candles.Given the heighti for each individual candle, find and print the number of candles she can successfully blow out.Input FormatThe first line contains a single integer, , denoting the number of candles on the cake. The second line contains  space-separated integers, where each integer  describes the height of candle .Output FormatPrint the number of candles Colleen blows out on a new line.Sample Input 043 2 1 3Sample Output 02Explanation 0We have one candle of height 1, one candle of height 2, and two candles of height 3. Colleen only blows out the tallest candles, meaning the candles where height=3. Because there are 2 such candles, we print 2 on a new line.My solution to the exercise above. You can find full code in gist with full codefunction birthdayCakeCandles(n, ar) {  var counts = {};  ar.forEach(function(x) {      counts[x] = (counts[x] || 0) + 1;  });  var toAr = Object.keys(counts).map(function(key){ return counts[key]; });  var result = Math.max.apply(Math, toAr);  return result;}n - number of candlesar - array with heights of the candlesI would like to get a feedback on my part of the code, as I guess I might do a lot of unnecessary steps.
HackerRank - Birthday Cake Candles and the counting loop
javascript;programming challenge
The algorithm is to count the number of elements in the array that are equal to the max of the array.  Let the code express that.  Both the original code and the procedural accepted answer are a clutter of bookkeeping details.function birthdayCakeCandles(n, ar) {    const max = Math.max(...ar)    return ar.filter(x => x === max).length}