Find min and max in array Java
In this post, you will learn how to find the min and max value in an array using Java programming.
Find min and max in array using forloop
In this given example, we have used the forloop to loop through the array and find the min and max value.
class FindMinMaxValue {
public static void main(String args[]){
int array[] = new int[]{5, 23, 32, 12, 90, 102};
// Calling getMaxValue() method for getting max value
int maxValue = getMaxValue(array);
System.out.println("Maximum Value is: "+maxValue);
// Calling getMinValue() method for getting min value
int minValue = getMinValue(array);
System.out.println("Minimum Value is: "+minValue);
}
// Method to find max value
public static int getMaxValue(int[] numbers){
int max = numbers[0];
for(int i=1;i < numbers.length;i++){
if(numbers[i] > max){
max = numbers[i];
}
}
return max;
}
// Method to find min value
public static int getMinValue(int[] numbers){
int min = numbers[0];
for(int i=1;i< min){
min = numbers[i];
}
}
return min;
}
}
Output of the above code:
Maximum Value is: 102
Minimum Value is: 5
Find min and max in array using collection
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in java.util package, it consists entirely of static methods which are used to operate on collections. In the given example, we have used the collection to find the min and max of an array.
import java.util.Arrays;
import java.util.Collections;
public class FindMinMax {
public static void main(String[] args)
{
// Initializing array
Integer[] array = { 62, 40, 17, 53, 72, 41, 102 };
// finding minimum element
// using Collections.min()
int min = Collections.min(Arrays.asList(array));
// finding maximum element
// using Collections.max()
int max = Collections.max(Arrays.asList(array));
// printing minimum and maximum numbers
System.out.println("Minimum number of array is : "
+ min);
System.out.println("Maximum number of array is : "
+ max);
}
}
Output of the above code:
Minimum number of array is : 17
Maximum number of array is : 102
Find min and max in array using recursion
Recursion function is a function which is called by itself. In the given example, we call the recursion function to get the min and max value. A recursion function continues until some condition is met to prevent it. In the given program, we use recursion to find the min and max value.
public class FindMinMax {
static int getMinValue(int arr[], int i, int n)
{
// find minimum of elements
return (n == 1) ? arr[i] : Math.min(arr[i],
getMinValue(arr,i + 1 , n - 1));
}
static int getMaxValue(int arr[], int i, int n)
{
// find maximum of elements
return (n == 1) ? arr[i] : Math.max(arr[i],
getMaxValue(arr ,i + 1, n - 1));
}
public static void main(String[] args)
{
int arr[] = { 53, 23, 19, 72, 97, 63 };
int len = arr.length;
System.out.print("Minimum element of array: " +
getMinValue(arr, 0, len) + "\n");
System.out.println("Maximum element of array: " +
getMaxValue(arr, 0, len));
}
}
Output of the above code:
Minimum element of array: 19
Maximum element of array: 97
Related Articles
Automorphic number in JavaPascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java