“Devolver números más grandes en matrices” Código de respuesta

Freecodecamp Algoritmo Basic Scripting Devuelve los números más grandes en matrices

function largestOfFour(arr) {
  var newArr = [];
for(var i = arr.length - 1; i >= 0; i--){
  arr[i].sort(function(a, b){return a - b});
newArr.push(arr[i][arr.length - 1])
}
 newArr.sort(function(a, b){return -b - a});
  return newArr;
}
Tired Tern

Devolver números más grandes en matrices

// 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;
		});
	});
}
YosKa

Devolver números más grandes en matrices

function largestOfFour(arr) {
  let result = []
  for (let i = 0; i < arr.length; i++) {
    let largest = arr[i][0]
    for (let j = 0; j < arr.length; j++) {
      if(arr[i][j] > largest) {
        largest = arr[i][j];
      }
    }
    result[i] = largest;
  }
  return result;
}

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

// Explanations:

/*  Create a variable to store the results as an array.
    Create an outer loop to iterate through the outer array.
    Create a second variable to hold the largest number and initialise it with the first number. This must be outside an inner loop so it won’t be reassigned until we find a larger number.
    Create said inner loop to work with the sub-arrays.
    Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
    After the inner loop, save the largest number in the corresponding position inside of the results array.
    And finally return said array. 
*/
Ralph Dizon

Encuentra el número más grande de una matriz

static int LargestNumberin(int[] numbers){
            int largestNumber = numbers[0];
            for (int i = 1; i < numbers.Length +1; i++)
            {
                if(numbers[i] > largestNumber){
                    largestNumber = numbers[i];
                }
            }
            return largestNumber;
        }
Energetic Eagle

Respuestas similares a “Devolver números más grandes en matrices”

Preguntas similares a “Devolver números más grandes en matrices”

Más respuestas relacionadas con “Devolver números más grandes en matrices” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código