### 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 :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; /* Java program to Find Largest and Smallest Number in an Array */ public class FindLargestSmallestNumberMain { public static void main(String[] args) { //array of 10 numbers int arr[] = new int[]{12,56,76,89,100,343,21,234}; //assign first element of an array to largest and smallest int smallest = arr[0]; int largest = arr[0]; for(int i=1; i< arr.length; i++) { if(arr[i] > largest) largest = arr[i]; else if (arr[i] < smallest) smallest = arr[i]; } System.out.println("Smallest Number is : " + smallest); System.out.println("Largest Number is : " + largest); } } |

1 2 3 4 |
Largest Number is : 343 Smallest Number is : 12 |