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
Answered by : light-lark-9pk4xs7vytyc
const numbers = [5, 10, 3, 8, 1, 9];
let largest = -Infinity; // Initialize largest to a very small value
let secondLargest = -Infinity; // Initialize secondLargest to a very small value
for (let i = 0; i < numbers.length; i++) { if (numbers[i] > largest) { // If current number is greater than largest, update both largest and secondLargest secondLargest = largest; largest = numbers[i]; } else if (numbers[i] > secondLargest && numbers[i] < largest) { // If current number is greater than secondLargest but smaller than largest, update secondLargest only secondLargest = numbers[i]; }
}
if (numbers.length >= 2) { console.log(secondLargest);
} else { console.log(null);
}
Source : https://chat.openai.com/ | Last Update : Tue, 25 Jul 23
Answered by : nice-narwhal-skg7pwi2jx67
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 : | Last Update : Sun, 30 May 21
Answered by : important-impala-fejwii22vago
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Source : https://stackoverflow.com/questions/17039770/how-do-i-get-the-second-largest-element-from-an-array-in-javascript/17039929 | Last Update : Fri, 02 Apr 21
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
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
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
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
Answered by : you
function getSecondLargestNumber(arr) { if (arr.length < 2) return "Not enough elements in the array to find the second largest number."; let max = Math.max(...arr); // Find the maximum number in the array let secondMax = Math.min(...arr); // Set the second max as the minimum possible value for (let i = 0; i < arr.length; i++) { if (arr[i] > secondMax && arr[i] < max) { secondMax = arr[i]; // Update the second max if a higher number is found } } return secondMax;
}
// Example usage:
const numbers = [4, 7, 2, 9, 5, 1, 8, 3, 6];
const secondLargest = getSecondLargestNumber(numbers);
console.log(secondLargest); // Output: 8
Source : | Last Update : Tue, 19 Sep 23
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