Skip to content

Commit 7c8a66f

Browse files
committed
--update: find 2nd max in array
1 parent 131a270 commit 7c8a66f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/_Problems_/find-2nd-max/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* You may find it easy but it's tricky for few
3+
* Input - [9, 2, 3, 6]
4+
* Output - 6
5+
*/
6+
7+
function findSecondMax(arr) {
8+
let max = arr[0];
9+
let max2 = 0;
10+
11+
for (let el of arr) {
12+
if (el > max) {
13+
max2 = max;
14+
max = el;
15+
}
16+
17+
if (el < max && el > max2) {
18+
max2 = el;
19+
}
20+
}
21+
return max2;
22+
}

0 commit comments

Comments
 (0)