-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDay3.Arrays.js
51 lines (36 loc) · 1.44 KB
/
Day3.Arrays.js
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
// 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
// , where is the number at index .
// The numbers in are not distinct.
// Output Format
// Return the value of the second largest number in the array.
// Sample Input 0
// 5
// 2 3 6 6 5
// Sample Output 0
// 5
// Explanation 0
// Given the array , we see that the largest value in the array is and the second largest value is . Thus, we return as our answer.
/**
* 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
let numsSort = nums.sort(function(a, b){return a-b});
let uniqueArr = [];
numsSort.forEach(function(element) {
if(uniqueArr.indexOf(element) == -1) {
uniqueArr.push(element);
}
});
return uniqueArr[uniqueArr.length - 2];
}