Finding Second Highest Number In Array

[Solved] Finding Second Highest Number In Array | Basic - Code Explorer | yomemimo.com
Question : find second largest number in array javascript

Answered by : jumping-boy

var secondMax = function (){ var arr = [20, 120, 111, 215, 54, 78]; // use int arrays var max = Math.max.apply(null, arr); // get the max of the array arr.splice(arr.indexOf(max), 1); // remove max from the array return Math.max.apply(null, arr); // get the 2nd max
};

Source : https://stackoverflow.com/questions/17039770/how-do-i-get-the-second-largest-element-from-an-array-in-javascript | Last Update : Wed, 21 Oct 20

Question : second largest number in array

Answered by : prashant-priyadarshi

public static void SecondndLargestfromarray()
{ var scan = new Scanner(System.in); var n = scan.nextInt(); long l=Long.MIN_VALUE; long p=Long.MIN_VALUE; for (int i = 0; i < n; i++) { var sc = scan.nextLong(); if(sc>l) { p=l; l=sc; } else if(sc>p ) p = sc; } System.out.println(p);
}

Source : | Last Update : Sat, 19 Nov 22

Question : finding second highest number in array

Answered by : asghar-abbas

public static int secHigh(int arr[]){ int firstHigh = 0,secHigh = 0; for(int x: arr){ if(x > firstHigh){ secHigh = firstHigh; firstHigh = x; }else if(x > secHigh){ secHigh = x; } } return secHigh;
}

Source : https://stackoverflow.com/questions/2615712/finding-the-second-highest-number-in-array/38942442#38942442 | Last Update : Mon, 14 Feb 22

Question : find second largest number in array javascript

Answered by : courageous-chamois-w8apjicmc4ih

var secondMax = function (arr){ var max = Math.max.apply(null, arr), // get the max of the array maxi = arr.indexOf(max); arr[maxi] = -Infinity; // replace max in the array with -infinity var secondMax = Math.max.apply(null, arr); // get the new max arr[maxi] = max; return secondMax;
};

Source : https://stackoverflow.com/questions/17039770/how-do-i-get-the-second-largest-element-from-an-array-in-javascript | Last Update : Thu, 01 Oct 20

Question : find the second largest number in an array javascript

Answered by : helpless-hummingbird-e6uo773fj7a8

function largestOfFour(mainArray) { return mainArray.map(function (subArray){ return subArray.reduce(function (previousLargestNumber, currentLargestNumber) { return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber; }, 0); });
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Source : https://www.freecodecamp.org/news/three-ways-to-return-largest-numbers-in-arrays-in-javascript-5d977baa80a1/ | Last Update : Fri, 28 Jan 22

Question : how to get second largest number in an array

Answered by : you

def find_second_largest(arr): if len(arr) < 2: print("Array should have at least two elements.") return largest = second_largest = float('-inf') for num in arr: if num > largest: second_largest = largest largest = num elif largest > num > second_largest: second_largest = num return second_largest
# Example usage:
numbers = [5, 10, 9, 12, 15, 3]
result = find_second_largest(numbers)
print("Second largest number:", result)

Source : | Last Update : Mon, 18 Sep 23

Question : How would you find the second largest number in an array?

Answered by : lohith

// Java program to find second largest
// element in an array
import java.util.*;
class GFG{
// Function to print the
// second largest elements
static void print2largest(int arr[], int arr_size)
{ int i, first, second; // There should be // atleast two elements if (arr_size < 2) { System.out.printf(" Invalid Input "); return; } // Sort the array Arrays.sort(arr); // Start from second last element // as the largest element is at last for (i = arr_size - 2; i >= 0; i--) { // If the element is not // equal to largest element if (arr[i] != arr[arr_size - 1]) { System.out.printf("The second largest " + "element is %d\n", arr[i]); return; } } System.out.printf("There is no second " + "largest element\n");
}
// Driver code
public static void main(String[] args)
{ int arr[] = {12, 35, 1, 10, 34, 1}; int n = arr.length; print2largest(arr, n);
}
}
// This code is contributed by gauravrajput1

Source : https://www.geeksforgeeks.org/find-second-largest-element-array/ | Last Update : Thu, 23 Jun 22

Question : Find second largest number in an array

Answered by : golu-rawat

// Java program to find second largest
// element in an array
import java.util.*;
class GFG{
 
// Function to print the
// second largest elements
static void print2largest(int arr[],
                          int arr_size)
{
  int i, first, second;
 
  // There should be
  // atleast two elements
  if (arr_size < 2)
  {
    System.out.printf(" Invalid Input ");
    return;
  }
 
  // Sort the array
  Arrays.sort(arr);
 
  // Start from second last element
  // as the largest element is at last
  for (i = arr_size - 2; i >= 0; i--)
  {
    // If the element is not
    // equal to largest element
    if (arr[i] != arr[arr_size - 1])
    {
      System.out.printf("The second largest " +
                        "element is %d\n", arr[i]);
      return;
    }
  }
 
  System.out.printf("There is no second " +
                    "largest element\n");
}
 
// Driver code
public static void main(String[] args)
{
  int arr[] = {12, 35, 1, 10, 34, 1};
  int n = arr.length;
  print2largest(arr, n);
}
}
 
// This code is contributed by gauravrajput1

Source : https://www.geeksforgeeks.org/find-second-largest-element-array/ | Last Update : Wed, 31 Aug 22

Question : find the second largest number in array javascript

Answered by : abhiiii

const input = ['20', '120', '111', '215', '54', '78'];
const secondSort = input.sort(function (a, b) { return b - a}[1])
console.log("===> :: secondSort", secondSort);

Source : | Last Update : Wed, 27 Apr 22

Answers related to finding second highest number in array

Code Explorer Popular Question For Basic