xxxxxxxxxx
function findSecondLargest(arr){
let largest=0, secondLargest = 0
for (i of arr){
if (i > largest){
largest = i
}
}
for (j of arr){
if(j>secondLargest && j<largest){
secondLargest = j
}
}
return secondLargest;
}
console.log("Second Largest",findSecondLargest([1,4,2,3,0]))
xxxxxxxxxx
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
};
xxxxxxxxxx
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);
}
xxxxxxxxxx
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
};
xxxxxxxxxx
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
xxxxxxxxxx
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;
}
xxxxxxxxxx
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;
};
xxxxxxxxxx
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]]);
xxxxxxxxxx
const arr = ['20','120','111','215','54','78', '120'];
let intArray = arr.map(Number); // convert into number
intArray = [new Set(intArray)]; // Remove duplicate numbers
const secondLargestNumber = intArray.sort((a,b) => {
return b - a;
})[1];
console.log(secondLargestNumber) // 120
xxxxxxxxxx
function nextBiggest(arr) {
let max = -Infinity, result = -Infinity;
for (const value of arr) {
const nr = Number(value)
if (nr > max) {
[result, max] = [max, nr] // save previous max
} else if (nr < max && nr > result) {
result = nr; // new second biggest
}
}
return result;
}
const arr = ['20','120','111','215','54','78'];
console.log(nextBiggest(arr));