xxxxxxxxxx
public static void main(String[] args) {
int[] xr = {2, 4, 1, 3, 7, 5, 6, 10, 8, 9};
//find maximum value
int max = xr[0];
for (int i = 0; i < xr.length; i++) {
if (xr[i] > max) {
max = xr[i];
}
}
//find minimum value
int min=xr[0];
for (int i = 0; i <xr.length ; i++) {
if (xr[i]<min){
min=xr[i];
}
}
System.out.println("max: "+max);
System.out.println("min: "+min);
}
xxxxxxxxxx
var numbers = [1, 2, 3, 4];
Math.max(numbers) // 4
Math.min(numbers) // 1
xxxxxxxxxx
// find maximum value of array in javascript
// array reduce method
const arr = [49,2,71,5,38,96];
const max = arr.reduce((a, b) => Math.max(a, b));
console.log(max); // 96
// math.max apply method
const max_ = Math.max.apply(null, arr);
console.log(max_); // 96
// or math.max spread operator method
const max__ = Math.max(arr);
console.log(max__); // 96
xxxxxxxxxx
int max;
max=INT_MIN;
for(int i=0;i<ar.length();i++){
if(ar[i]>max){
max=ar[i];
}
xxxxxxxxxx
function arrayMin(arr) {
return arr.reduce(function (p, v) {
return ( p < v ? p : v );
});
}
function arrayMax(arr) {
return arr.reduce(function (p, v) {
return ( p > v ? p : v );
});
}
xxxxxxxxxx
function findMinMax(arr) {
if (arr.length === 0) {
return "Array is empty.";
}
let min = arr[0];
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return { min, max };
}
// Example usage:
const numbers = [11, 5, 8, 3, 20, 16];
const result = findMinMax(numbers);
console.log(result); // Output: { min: 3, max: 20 }
xxxxxxxxxx
console.log(Math.max(1, 3, 2));
// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(array1));
// expected output: 3