Skip to content

Commit 6f01236

Browse files
authored
Create Arrays.js
1 parent e522d81 commit 6f01236

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
process.stdin.resume();
4+
process.stdin.setEncoding('utf-8');
5+
6+
let inputString = '';
7+
let currentLine = 0;
8+
9+
process.stdin.on('data', inputStdin => {
10+
inputString += inputStdin;
11+
});
12+
13+
process.stdin.on('end', _ => {
14+
inputString = inputString.trim().split('\n').map(string => {
15+
return string.trim();
16+
});
17+
18+
main();
19+
});
20+
21+
function readLine() {
22+
return inputString[currentLine++];
23+
}
24+
25+
/**
26+
* Return the second largest number in the array.
27+
* @param {Number[]} nums - An array of numbers.
28+
* @return {Number} The second largest number in the array.
29+
**/
30+
function getSecondLargest(nums) {
31+
// Complete the function
32+
let first=nums[0];
33+
let second=-1;
34+
for(let i=0;i<nums.length;i++)
35+
{
36+
if(nums[i]>first)
37+
{
38+
second=first;
39+
first=nums[i];
40+
}
41+
if(nums[i]>second && nums[i]<first)
42+
{
43+
second=nums[i];
44+
}
45+
}
46+
return second;
47+
}
48+
49+
50+
function main() {
51+
const n = +(readLine());
52+
const nums = readLine().split(' ').map(Number);
53+
54+
console.log(getSecondLargest(nums));
55+
}

0 commit comments

Comments
 (0)