'HackerRank - Birthday Cake Candles
I was just practising Javascript from Hackerrank. The question that I am working on is as follows:
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.
I executed the code below and the right answer was reflected in the console logs. However, when I tried to run the code on Hackerrank, it said that my answer was undefined. Why is that so?
function birthdayCakeCandles(ar) {
var maxHeight = Math.max(...ar);
var maxHeightCount = 0;
for(var i = 0; i < ar.length; i++){
if (ar[i] == maxHeight){
maxHeightCount = maxHeightCount + 1;
}
}
console.log(maxHeightCount);
}
Solution 1:[1]
Trying returning the value:
`
function birthdayCakeCandles(ar) {
var maxHeight = Math.max(...ar);
var maxHeightCount = 0;
for(var i = 0; i < ar.length; i++){
if (ar[i] == maxHeight){
maxHeightCount = maxHeightCount + 1;
}
}
console.log(maxHeightCount);
return maxHeightCount;
}
`
Solution 2:[2]
function birthdayCakeCandles(arr) {
const velas = arr.filter(value => Math.max(...arr) === value)
return velas.length;
}
Solution 3:[3]
function birthdayCakeCandles(candles) {
var max_candles = Math.max(...candles)
var number_candles = 0
for(var i=0; i<candles.length; i++){
if(candles[i] == max_candles){
number_candles = number_candles + 1
}
}
return number_candles;
}
Solution 4:[4]
function birthdayCakeCandles(candles) {
// Write your code here
let max = candles.reduce((a, b)=>Math.max(a, b));
return candles.filter(x => x == max).length
}
Solution 5:[5]
Try this solution:
function birthdayCakeCandles(candles) {
let tallestCount = 1;
let i = 0;
candles.sort((a, b) => b - a);
while (i < candles.length) {
if (candles[i] === candles[i + 1]) {
tallestCount++
i++;
} else {
break;
}
}
return tallestCount;
}
Solution 6:[6]
Here is my solution which had passed all the test cases.
function birthdayCakeCandles(candles) {
const bigCandle = Math.max(...candles);
const count = candles.filter(x => x === bigCandle).length;
return count;
}
Solution 7:[7]
Here is my solution with the Array Reduce method
function birthdayCakeCandles(candles) {
let maxHeight = Math.max.apply(null, candles);
let totalTallCandles = candles.reduce((cum, cur) => {
if (cur == maxHeight) {
cum += 1;
}
return cum;
}, 0);
return totalTallCandles;
}
let total = birthdayCakeCandles([3, 5, 2, 1, 6, 5, 6]);
alert(total);
Solution 8:[8]
will its an old thread and already answered it but I have anthor solution for you
function birthdayCakeCandles(candles) {
arr.sort().reverse();
const tallCandle = arr[0]
const arrIndex = arr.length -1
let sum = 0
for (i=0;i<=arrIndex;i++){
if(arr[i] === tallCandle){
sum += 1
}
}
return sum
}
Solution 9:[9]
Python Version
def birthdayCakeCandles(candles):
# Write your code here
counter = {}
for candle in candles:
if candle in counter:
counter[candle] += 1
else:
counter[candle] = 1
max_number = max(candles)
return counter.get(max_number)
Solution 10:[10]
function birthdayCakeCandles(candles) {
// Write your code here
let maxCandle = candles.sort().reverse()[0];
return candles.filter(x => x === maxCandle).length;
}
Solution 11:[11]
function birthdayCakeCandles(candles){
// Write your code here
let tallestCandles = 1; //There is at least one(1) tallest candle
let sortedArr = candles.sort();
let index = sortedArr.length-1;
while (sortedArr[index]==sortedArr[index-1] && index>=0) {
tallestCandles++;
index--;
}
return tallestCandles;
}
Solution 12:[12]
easy solution
const birthdayCakeCandles = candles => {
let max = 0;
candles.forEach(candle => candle > max ? max = candle : max); //find max number
const result = candles.filter(candle => candle === max); //filter the same number as max
console.log(result.length);
}
birthdayCakeCandles([3,2,1,3]);
//result : 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow