Java Program to Find Smallest and Largest Element in an Array

Previous
Next
In this post, we will see how to find smallest and largest element in an array.

Problem:

You are given an array of numbers. You need to find smallest and largest numbers in the array.

Solution:

  • Initialise two variable largest and smallest with arr[0]
  • Iterate over array
    • If current element is greater than largest, then assign current element to largest.
    • If current element is smaller than smallest, then assign current element to smallest.
  • You will get smallest and largest element in the end.

Java code to find Smallest and Largest Element in an Array :

When you run above program, you will get below output:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog