-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbirthdayCakeCandles.js
More file actions
58 lines (42 loc) · 981 Bytes
/
Copy pathbirthdayCakeCandles.js
File metadata and controls
58 lines (42 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Complete the 'birthdayCakeCandles' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY candles as parameter.
*
*Approach 2 (most efficient)
Research:
single-pass loop
conditional counting
Conceptual flow:
While looping:
track current tallest height
track count of tallest
If you find:
a taller candle → reset count to 1
equal height → increment count
This avoids:
* creating an object
* creating extra arrays
* multiple passes
*
This is usually considered the most optimal solution algorithmically.
*
*
*
*
*
*
*/
let candles = [3,2,1,3];
function birthdayCakeCandles(candles) {
// Write your code here
let candleObj = {};
for (let element of candles) {
candleObj[element] = (candleObj[element] || 0) + 1
}
const objValues = Object.values(candleObj);
const maxValue = Math.max(...objValues)
console.log(maxValue)
};
console.log(birthdayCakeCandles(candles));