forked from nathan-abela/HackerRank-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09 - Day 3 - Arrays.js
More file actions
36 lines (28 loc) · 853 Bytes
/
09 - Day 3 - Arrays.js
File metadata and controls
36 lines (28 loc) · 853 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
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/js10-arrays/problem
// Difficulty: Easy
// Max Score: 15
// Language: JavaScript (Node.js)
// ========================
// Solution
// ========================
// Return the second largest number in the array.
// @param {Number[]} nums - An array of numbers.
// @return {Number} The second largest number in the array.
function getSecondLargest(nums) {
// Complete the function
nums.sort((a, b) => a < b); // This sorts inversely
var a = nums.shift();
while (a == nums[0]) {
a = nums.shift();
}
a = nums.shift();
return a;
}
function main() {
const n = +(readLine());
const nums = readLine().split(' ').map(Number);
console.log(getSecondLargest(nums));
}