-1

How do you get the 2nd highest and lowest numbers in javascript ? Without using sort or any other functions in js. I actually got highest and the lowest but I really don't know what to fix to get the 2nd numbers. this is my code

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script type="text/javascript">

function myFunction(){
 var numone = document.getElementById("num1").value;
 var numtwo = document.getElementById("num2").value;
 var numthree = document.getElementById("num3").value;
 var numfour = document.getElementById("num4").value;
 var numfive = document.getElementById("num5").value;
  
 var array = [numone,numtwo,numthree,numfour,numfive];


var largest= 0;

for (i=0; i<=array.length;i++){
    if (array[i]>largest) {
        var largest=array[i];
    }
    
}
    document.getElementById("largest").value = largest;

var smallest=array[0];

for (i=0; i<array.length; i++){
 if(array[i]<smallest){
  var smallest=array[i];
}
    document.getElementById("lowest").value = smallest;
}

}

</script>
</head>
<body>

<input type="number" id="num1">
<input type="number" id="num2">
<input type="number" id="num3">
<input type="number" id="num4">
<input type="number" id="num5">

<button type="button" onclick="myFunction()" class="btn btn-default">SUBMIT</button>
<input type="text" id="largest" disabled>
<input type="text" id="lowest" disabled>
<script type="text/javascript">
myFunction();
</script>
</body>
</html>
  • Look around the site : https://stackoverflow.com/a/32341832/6753550 – Alexandre Thyvador Aug 10 '17 at 06:50
  • It should be simple if you sort it using your own code like bubble sort (https://stackoverflow.com/questions/7502489/bubble-sort-algorithm-javascript) on the value in asc order, 2nd highest `array[arraylenght-2]`, `lowest :array[0]`, you need to exception handling when array is empty or has only one item – Anil Aug 10 '17 at 06:52
  • the ques specifies without using sort – marvel308 Aug 10 '17 at 06:53
  • the rule is, I can't use any functions like max, min or sort... – Jonathan Aug 10 '17 at 06:53
  • 1
    Possible duplicate of [Given an array of integers, find the second largest and second smallest within the array](https://stackoverflow.com/questions/32340896/given-an-array-of-integers-find-the-second-largest-and-second-smallest-within-t) – Dij Aug 10 '17 at 06:53
  • It's very difficult to find the second largest and second smallest from an unsorted array. If you have success with some sort of data may be break on another one. If you can sort it, you can also avoid unwanted condition checking too. According to the data structure theory, we should first sort an array before doing such operations. – Libin C Jacob Aug 11 '17 at 07:20

1 Answers1

0

You can do this in the following way

let arr = [1, 2, 3, 4, 5];
let maxx = arr[0], idxMaxx = 0;
let minn = arr[0], idxMinn = 0;
 
for(let i=0; i<arr.length; i++){
 if(arr[i] > maxx){
  maxx = arr[i];
  idxMaxx = i;
 }
 if(arr[i] < minn){
  minn = arr[i];
  idxMinn = i;
 }
}
 
let Secondmaxx = -1, idxSecondMaxx = -1;
let Secondminn = -1, idxSecondMinn = -1;
 
for(let i=0; i<arr.length; i++){
 if(arr[i] > Secondmaxx && i!= idxMaxx){
  Secondmaxx = arr[i];
  idxSecondMaxx = i;
 }
 if(Secondminn == -1 && i != idxMinn){
  Secondminn = arr[i];
  idxSecondMinn = i;
 }
 if(arr[i] < Secondminn){
  Secondminn = arr[i];
  idxSecondMinn = i;
 }
}
 
console.log(Secondmaxx, idxSecondMaxx);
console.log(Secondminn, idxSecondMinn);

Time complexity O(n) where n is the number of elements in the array

marvel308
  • 9,972
  • 1
  • 18
  • 31