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

How would you find the 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 :: next js latest 
Javascript :: jQuery Method Chaining 
Javascript :: change form value dynamically angular 
Javascript :: The toString() Method 
Javascript :: fibonacci sequence javascript 
Javascript :: what is const in javascript 
Javascript :: hasChildNodes 
Javascript :: vuetify use selected value 
Javascript :: how to parse header in node.js lambda 
Javascript :: nodejs: router by use express and path package 
Javascript :: jquery find element 
Javascript :: Use the parseInt Function 
Javascript :: append item to array javascript 
Javascript :: change parent state from child use effect in react js 
Javascript :: Fibonacci , fibo 
Javascript :: js rename property 
Javascript :: typescript deserialize json 
Javascript :: promise definition in javascript 
Javascript :: javascript url replace 
Javascript :: calling javascript from java 
Javascript :: clickable 
Javascript :: search for diff in two JSON 
Javascript :: js listen websocket 
Javascript :: datatable hide no data available in table 
Javascript :: remove last character from string javascript 
Javascript :: this keyword in javscript 
Javascript :: comming soon page in react 
Javascript :: react native stepper 
Javascript :: axios delete 
Javascript :: ng2-tel-input phone number code 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =