DEV Community

tandu-dev
tandu-dev

Posted on

HackerRank Birthday Cake Candles challenge

HackerRank has a challenge for calculating which birthday candles get blown out.

My C# solution looked particularly elegant to me, so I thought I'd show it here:

public static int birthdayCakeCandles(List<int> candles)
{
        var maxHeight = candles.Max();
        var numOfCandles = candles.Count(c => c == maxHeight);
        return numOfCandles;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
pbouillon profile image
Pierre Bouillon

Hey @tandu-dev, nice solution, thanks for sharing!

I wonder if using the GroupBy would have save some iterations here

public static int birthdayCakeCandles(List<int> candles)
    => candles
        .GroupBy(x => x)
        .OrderByDescending(x => x.Key)
        .First()
        .Count();
Enter fullscreen mode Exit fullscreen mode

A dictionary might also be a good fit for such a problem!

Collapse
 
tandudev profile image
tandu-dev

That's definitely a workable solution? Could you go into it a little more? I'm familiar with the => notation for Linq, but haven't seen it used with the method this way. Is this more functional programming?

Collapse
 
pbouillon profile image
Pierre Bouillon

Kind of, those are just syntactic sugar, it is a body expression 😄