DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 Author

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 πŸ˜„

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: