Find the Second Largest Element in an Array


/**

  • In main file
  • let script1 = require('./script1');
  • console.log(script1.sum(1, 2));
    */
    let arr = [5, 1];
    function sum(a) {
    //we have to find the highest amongst all
    //create a flag highest no
    let highest = null;
    //create second highest flag
    let secondHighest = null;
    //loop 1 to iterate all
    for(let i=0;i<a.length-1;i++){
    //loop2 to take a number and compare with other iterable array numbers
    for(let j=0;j<a.length-1;j++){
    if(a[j] > a[i] && a.length > 2){
    //set as we find the highest once all iteration end we have the highest one.
    //set second highest flag if we find one.
    secondHighest = highest;
    highest =a[j];
    }
    }
    }
    return Highest: ${highest}, Second Highest: ${secondHighest};
    }

console.log(sum(arr));