HackerRank - Birthday Cake Candles and the counting loop I was having fun on solving one of the HackerRank simple challenges
- Birthday Cake Candles. 



Description


  Colleen 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 Format

The 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 Format

Print the number of candles Colleen blows out on a new line.



Sample Input 0

4
3 2 1 3


Sample Output 0

2


Explanation 0

We 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 code

function 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 candles
ar - array with heights of the candles




I would like to get a feedback on my part of the code, as I guess I might do a lot of unnecessary steps.
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 =&gt; x === max).length
}