Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find second largest number in array javascript

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
};
Comment

javascript find the second highest Element from array

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
};
Comment

second largest number in array javascript

['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Comment

finding second highest number in array

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;
}
Comment

find second largest number in array javascript

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;
};
Comment

find the second largest number in an array javascript

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]]);
Comment

Find second largest number in an array

// 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
", arr[i]);
      return;
    }
  }
 
  System.out.printf("There is no second " +
                    "largest element
");
}
 
// 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
Comment

find the second largest number in array javascript

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

PREVIOUS NEXT
Code Example
Javascript :: uuid react native expo 
Javascript :: javascript pipe function 
Javascript :: regex validations 
Javascript :: angularjs ng-options name value 
Javascript :: Discord.client on 
Javascript :: sequelize date format 
Javascript :: export multiple function in node js 
Javascript :: label animation css 
Javascript :: map and reduce an array in js 
Javascript :: how to append a data to list in redux 
Javascript :: javascript factory functions 
Javascript :: Iterate with JavaScript Do...While Loops 
Javascript :: add style by classname javascript 
Javascript :: angular $http abort request 
Javascript :: javascript force view to focus on a div 
Javascript :: get decimals from float javascript 
Javascript :: mobile detect js 
Javascript :: duplicate text javascript 
Javascript :: math.floor + roandom 
Javascript :: await javascript 
Javascript :: useref example 
Javascript :: chatbot using html and javascript 
Javascript :: react animate on scroll 
Javascript :: how to get bearer token in react 
Javascript :: what does connect do in redux 
Javascript :: display fetch response js 
Javascript :: javascript map replace key value 
Javascript :: how to check if it is the current time day.js 
Javascript :: vue3 header 
Javascript :: auto generate component angular 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =