-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathday3-arrays.js
More file actions
35 lines (28 loc) · 1.1 KB
/
Copy pathday3-arrays.js
File metadata and controls
35 lines (28 loc) · 1.1 KB
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
/*
Objective
In this challenge, we learn about Arrays. Check out the attached tutorial for more details.
Task
Complete the getSecondLargest function in the editor below. It has one parameter: an array, , of numbers. The function must find and return the second largest number in .
Input Format
- Locked stub code in the editor reads the following input from stdin and passes it to the function:
- The first line contains an integer, , denoting the size of the array.
- The second line contains space-separated numbers describing the elements in .
Constraints
- 1 <= n <= 100
- 0 <= nums[i] <= 100, where nums[i] is the number at index i.
- The numbers in nums are not distinct.
Output Format
Return the value of the second largest number in the nums array.
*/
function getSecondLargest(nums) {
let max = nums[0], secondMax = nums[0];
for (let i = 0; i <= nums.length; i++) {
if (i < nums.length && nums[i] > max) {
max = nums[i];
}
if (i > 0 && nums[i - 1] > secondMax && nums[i - 1] < max) {
secondMax = nums[i - 1];
}
}
return secondMax;
}