Answers for "3rd largest element in an array"

0

2nd highest number from array

var arr = [1,2, -3, 15, 77, 12, 55];
var highest = 0, secondHighest = 0;
// OR var highest = arr[0], secondHighest = arr[0];

for(var i=0; i<arr.length; i++){
  if(arr[i] > highest){
    secondHighest = highest;
    highest = arr[i]; 
  }

  if(arr[i] < highest && arr[i] > secondHighest){
    secondHighest = arr[i];
  }
}

console.log('>> highest number : ',highest); // 77
console.log('>> secondHighest number : ',secondHighest); // 55
Posted by: Guest on October-28-2021
0

Return Largest Numbers in Arrays

// Return Largest Numbers in Arrays

function largestOfFour(arr) {
	let largestArr = [];
	for (let i = 0; i < arr.length; i++) {
		largestArr.push(Math.max(...arr[i]));
	}
	return largestArr;
}

largestOfFour([
	[4, 5, 1, 3],
	[13, 27, 18, 26],
	[32, 35, 37, 39],
	[1000, 1001, 857, 1],
]);

// OR

function largestOfFour(arr) {
	return arr.map(function (group) {
		return group.reduce(function (prev, current) {
			return current > prev ? current : prev;
		});
	});
}
Posted by: Guest on January-02-2022

Code answers related to "3rd largest element in an array"

Code answers related to "BASIC"

Browse Popular Code Answers by Language