Java Program to Find Smallest and Largest Element in an Array

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:

import_contacts

You may also like:

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.