Java Program to Find Maximum And Minimum Values in an Array

To find maximum and minimum values in an array in Java you can use one of the following options-

  1. Iterate the array and look for the maximum and minimum values.
  2. You can also write a recursive method to recursively go through the array to find maximum and minimum values in an array.
  3. You can use Arrays.sort() method to sort the array and then take the first and last elements of that sorted array.

Iterating array to find largest and smallest values

Start by having two variables (max and min) with initial value as the first element of the array. Iterate through the array and compare current element with max variable, if current array element is greater than the max then assign the current element to max. Otherwise compare element with min variable, if current array element is less than the min then assign the current element to min.

Output

Find maximum and minimum values in array using recursion

Output

Find maximum and minimum values in array by sorting

Output

That’s all for the topic Java Program to Find Maximum And Minimum Values in an Array. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.