Get higher value in an array of numbers

2

Through given a function, I return an array with numerous random numbers. I need to get the largest number between this array, and for this I ended up finding the Math.max() function. The problem is that I can not get it to read my array and then return the maximum value ... I'm basically doing this:

var retorno = [3,6,9,22,46,73];
var maiorNumero = Math.max(retorno); //retornaria 73

Could someone present me some solution to the fact?

    
asked by anonymous 25.05.2015 / 21:10

2 answers

3

I found (rs), within Math.max() I have a variation to work on programmatic arrays that works as follows:

var retorno = [3,6,9,22,46,73];
var maiorNumero = Math.max.apply(Math,retorno); //ai sim retorna 73!!
    
25.05.2015 / 21:18
1

Another simple solution would be:

int max = array.get(0);

for ( int i = 1; i < array.length; i++) {
    if ( array.get(i) > max) {
      max = array.get(i);
    }
}
    
26.05.2015 / 13:35