Arrays-Max Value In Array code example

Example 1: max value in array javascript

// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });

// For arrays with relatively few elements you can use apply: 
const max = Math.max.apply(null, arr);

// or spread operator:
const max = Math.max(...arr);

Example 2: get max element of array javascript

const maxMinArray = (a,b,...c) =>{
    //we are concatinating this array
    let array = [a,b];
    let newArray = array.concat(c);
    //now let's get started with the excersice
    let max = 0;
    for(let number of newArray){
        if(max<number){
            max = number;
        }
    }
    let min = max;
    for(let number of newArray){
        if(number<min){
            min = number;
        }
    }
    return console.log(max + ' ' + min)
}
//example: 
maxMinArray(12,14,15,23)

Example 3: find the maximum number from an int Array

---without sort method---
public static int maxValue( int[]  n ) {

int max = Integer.MIN_VALUE;

for(int each: n)

if(each > max)

max = each;

 
return max;
  
---with sort method---
public static int maxValue( int[]  n ) {

Arrays.sort( n );

return  n [ n.lenth-1 ];

}

Example 4: max element in array

int max;
max=INT_MIN;

for(int i=0;i<ar.length();i++){
	if(ar[i]>max){
    	max=ar[i];
    }