Day 3: Arrays | 10 Days of JavaScript – Hacker Rank Solution

Day 3: Arrays | 10 Days of JavaScript – Hacker Rank Solution: Hey Guy’s In this post we are solving Day 3: Arrays which is a part of 10 Days of JavaScript Series.

Day 3: Arrays | 10 Days of JavaScript – Hacker Rank Solution

Objective

In this challenge, we learn about Arrays. Check out the attached tutorial for more details.

Function Description

Complete the getSecondLargest function in the editor below.

getSecondLargest has the following parameters:

  • int nums[n]: an array of integers

Returns

  • int: the second largest number in nums

Input Format

The first line contains an integer, n, the size of the nums array.
The second line contains n space-separated numbers that describe the elements in nums.

Constraints

  • 1 <= n <= 10
  • 0 <= numsi <= 100, where numsi is the number at index i.
  • The numbers in nums may not be distinct.

Sample Input 0

5

2 3 6 6 5

Sample Output 0

5

Explanation 0

Given the array nums = [2, 3, 6, 6, 5], we see that the largest value in the array is 6 and the second largest value is 5. Thus, we return 5 as our answer.

Day 3: Arrays | 10 Days of JavaScript – Hacker Rank Solution

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
});
function readLine() {
    return inputString[currentLine++];
}
/**
*   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
    var sArray = nums.sort(function (a,b){
        return a - b;
    });
    var uSarray = sArray.filter(function (elm, index, self){
        return index == self.indexOf(elm);
    });
    return uSarray[uSarray.length - 2];
}
function main() {
    const n = +(readLine());
    const nums = readLine().split(' ').map(Number);
    console.log(getSecondLargest(nums));
}

Disclaimer: The above Problem (Day 3: Arrays) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment