Reputation: 3
You are in charge of the cake for a child's birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.
Example
Candles = [4,4,1,3] The maximum height of candles is 4 units high. There are 2 of them, so return 2.
So basically the way I'm doing so is by moving from each place of the array comparing each other with two for cycles, the second cycle will count the repeated numbers, some people use Math.max imported function but I didn't know it before started looking for the answer, and I think this way should work, but can't get to the answer, any ideas?
function birthdayCakeCandles(candles) {
let height=1;
let b=0;
for (let i=0; i<candles.length; i++)
{
for (b=0; b<candles.length; b++)
{
if(b!=i && candles[b]===candles[i])
{height++;}
b++;
}
}
return height;
Upvotes: 0
Views: 3367
Reputation: 91
It can be easily solved as below:
public int birthdayCakeCandles (candles) {
int minValue = Integer.MIN_VALUE;
int result = 0;
for (Integer i : candles) {
if (minValue > i) {
continue;
} else if (minValue < i) {
result = 1;
minValue = i;
} else {
result++;
}
}
return result;
}
Upvotes: 0
Reputation: 37
You can try this below. The logic that i used is that first store the values in newArr[] with candles[0] and Highest numbers from candles[] array. It is because the logic used in first for loop that compares max_val = candles[0] with candles[i]. And so when storing max_val to newArr[], it taking candles[0] and its comparing values (equal to or greatern than candles[0]) along with largest numbers in candles[] array.
Now the second for() loop filtering values based on max_val == newArr[j]. The max_val in first for() loop already loaded with largest value, so that after comparison only largest numbers are filtered and stored into resultArr[]. Then return the function with length property.
var max_val = candles[0];
var newArr = [];
var resultArr = [];
var resultMax;
for(var i = 0; i < candles.length; i++)
{
if(max_val <= candles[i])
{
max_val = candles[i];
newArr.push(candles[i]);
}
}
for(var j = 0; j < newArr.length; j++)
{
if(max_val == newArr[j])
{
resultArr.push(newArr[j]);
}
}
var num = resultArr.length;
return num;
Upvotes: 0
Reputation: 81
well I solved that challenge with this
function birthdayCakeCandles(candles) {
const maxVal = Math.max(...candles)
//use an array to save all the biggest/max number
const newArr = []
for(let val of candles){
if(val === maxVal) {
newArr.push(val)
}
}
return newArr.length
}
But If you don't want to use Math.max() we have to find biggest value first. we can use for of loop to find max/biggest value like this:
function birthdayCakeCandles(candles) {
let maxVal = 0
//use loop to find the biggest number
for(let val of candles) {
if(val > maxVal) {
maxVal = val
}
if(val < maxVal) {
maxVal = maxVal
}
if(val === maxVal) {
maxVal = maxVal
}
}
//use an array to save all the biggest/max number
const newArr = []
for(let val of candles){
if(val === maxVal) {
newArr.push(val)
}
}
return newArr.length
}
Upvotes: 0
Reputation: 3
I solved it this way
function birthdayCakeCandles(candles) {
let height=0;
let max=Math.max(...candles);
for (let i=0; i<candles.length; i++)
{
if (candles[i]==max)
{height++;}
}
return height;
}
Upvotes: 0
Reputation: 1057
This should be straightforward, iterate through the array find max
if max
exists again increase count
, if some other element greater than max
set it to max
and reset count to 1
function findTallestCandleCount(candles){
let max = 0, count = 0
for (let candle of candles) {
if (candle > max) {
max = candle
count = 1
} else if (candle === max){
count++
}
}
return count;
}
Upvotes: 4