5

Okay so I'm learning Javascript with HackerRank's 10 days of Javascript, I'm at Day 3: which consists in Array problems. The current problem consists of returning the second-highest value from two inputs, the first is the amount of items on the array, and the second is an amount of values which will be saved at the 'nums' array. I made it with the code below, but only with the following input case, if I try the same code for different inputs (see at the end) it return to me 'Wrong Answer'. Here you can read more about the problem: https://www.hackerrank.com/challenges/js10-arrays/problem

'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++];
}

/* This is the code that I wrote */
function getSecondLargest(nums) {
    nums.sort();
    let maxValue = Math.max(...nums);
    let count = 0;
    for (let i=0;i<nums.length;i++){
        if (nums[i]==maxValue){
            count++;
        }
    }
    let secondMaxValue = nums[(nums.lastIndexOf(maxValue)-count)];
    return secondMaxValue;
}


function main() {
    const n = +(readLine());
    const nums = readLine().split(' ').map(Number);

    console.log(getSecondLargest(nums));
}
5

2 3 6 6 5

But when I try the same code with the following inputs I get "Wrong Answer"

10

1 2 3 4 5 6 7 8 9 10

Any explanation or maybe an advice please?

  • Please review [MCVE] guidance on posting code and [edit] question accordingly. Ideally you remove all input related code from example and show failure with hardcoded input. Also there is absolutely nothing in the code shown that can print "Wrong Answer" text - it is better to make sure that outputs/error message that you see match output of the code in the post. – Alexei Levenkov Jun 20 '20 at 04:02
  • https://stackoverflow.com/a/68708045/12401517 Vist this link for this problem – keshari abeysinghe Aug 09 '21 at 07:45

2 Answers2

3

The problem is sort() function. The javascript's sort() method will order the integer array in following way.

[1,10,2,3,4,5,6,7,8,9]

You need to use following method to sort it,

nums = nums.sort(function(a,b) {
       return (+a) - (+b);
    });

The sorting problem already asked here

BadPiggie
  • 5,471
  • 1
  • 14
  • 28
  • Hi I am stuck on the same problem but I am using a different technique. I first sorted my array and then made the array have unique elements using Set. Then I logged the second last element. Logic wise it should work. Here is the fiddle link of my code : https://jsfiddle.net/geomukkath/b3ov1yje/21/ I am getting undefined in hackerrank but it works properly in fiddle. In hackerrank I am getting the result and undefined. – Geo Mukkath Jan 21 '21 at 07:16
1

You just need to sort the numbers and get the second index. Follow the code.

function getSecondLargest(nums) {
   nums.sort((a, b) => {
     return b - a;
   }); 
   return nums[1]  
}

function getSecondLargest(nums) {
  nums.sort((a, b) => Math.sign(b - a));
  return nums[1]  
}

console.log(getSecondLargest([1,2,3,4,5,6,7,8,9,10]));
sibabrat swain
  • 1,277
  • 8
  • 20