Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

2nd highest number from array

var arr = [1,2, -3, 15, 77, 12, 55];
var highest = 0, secondHighest = 0;
// OR var highest = arr[0], secondHighest = arr[0];

for(var i=0; i<arr.length; i++){
  if(arr[i] > highest){
    secondHighest = highest;
    highest = arr[i]; 
  }

  if(arr[i] < highest && arr[i] > secondHighest){
    secondHighest = arr[i];
  }
}

console.log('>> highest number : ',highest); // 77
console.log('>> secondHighest number : ',secondHighest); // 55
Comment

2nd highest number from array


 static int secondHighest(int... nums) {
    int high1 = Integer.MIN_VALUE;
    int high2 = Integer.MIN_VALUE;
    for (int num : nums) {
      if (num > high1) {
        high2 = high1;
        high1 = num;
      } else if (num > high2) {
        high2 = num;
      }
    }
    return high2;
 }

Comment

PREVIOUS NEXT
Code Example
Javascript :: Capitalize first letter of string on json sending 
Javascript :: check box jquery 
Javascript :: javascript rock paper scissors 
Javascript :: array put value in index 
Javascript :: javascript take picture from camera 
Javascript :: set lodash 
Javascript :: react extends component construtor super props 
Javascript :: data-dismiss="modal" in js 
Javascript :: wordpress ajax trigger code 
Javascript :: print js 
Javascript :: how to delay something in javascript 
Javascript :: react bootstrap cdn 
Javascript :: get array of selected options from select element 
Javascript :: jquery load 
Javascript :: append after div 
Javascript :: angular read config from json 
Javascript :: try catch in react native 
Javascript :: How to Submit Forms and Save Data with React.js 
Javascript :: lodash reduce 
Javascript :: sequelize mariadb example 
Javascript :: how to clone an object 
Javascript :: get data from url using react 
Javascript :: js end of string 
Javascript :: javascript timestamp to date 
Javascript :: JavaScript querySelector - By ID 
Javascript :: node js send javascript 
Javascript :: jquery check if document loaded 
Javascript :: vue setup https 
Javascript :: scroll load react 
Javascript :: external script in react 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =