So, what we need to do is quite simple: create a function who accepts an array of numbers(n) and return the count of the largest number(s). Like,
constraints
1 ≤ 5 ≤ 10^5
1 ≤ candles[i] ≤ 10^7input: [1,3,55,12,21]
return: 1input: [3, 11, 35, 56, 12, 56]
return: 2input: [5, 13, 13, 13, 12, 7]
return: 3
Savvy?
Right, I will do it my way first(two of them), and check out how others solve this afterwards to show you the difference between an ordinary coder and those extremely smart hackers.
My first attempt with pretty straight-forward logic who looks a bit messy but it works.
And my second attemp with a totally different way.
Alright, let’s do some comparison.
And I find someone doing something like this, good solve by using Math
, Array.filter
and some currying. Basically, he finds the tallest candles by using Math.max
and filter
the array with the value equal to the max.
Let’s break it down a bit by extending the function.
Because birthdayCakeCandles
will return another function who needs a parameter called f
, and that is where the Math.max(…a)
will go inside the seconde ()
in the one-liner.
Alright, that’s all for today.