Find Largest Number in Array

Find largest number in array
Here is the most common and simple logic to find the largest number in array. The following program consists of a method called findLargest() which takes three parameters namely start, end and array respectively. The start represents the start index from which the hunt for the largest number should start and end represents the end index where the hunt should stop and the array is where the number has to be hunted. This logic is not only easy but also great. Learning it is a lot of fun. Just go through the program.

import java.util.*;
class FindLargeNumber
{
    public static void main(String args[])
    {
    Scanner s=new Scanner(System.in);
   
    System.out.println("Enter the no.of elements");
    int n=s.nextInt();

    int[] a=new int[n];
    System.out.println("Enter the elements into the array");
        for(int i=0;i<n;i++)
        {
        a[i]=s.nextInt();
        }

    int large=findLargest(0,a.length,a);
   
    System.out.println("The large number is "+large);
    }

    public static int findLargest(int start,int end,int[] a)
    {
    int large=a[start];
        for(int i=start+1;i<end;i++)
        {
            if(a[i]>large) large=a[i];
        }
    return large;
    }
}

Sample Output of the program


Enter the no.of elements
5
Enter the elements into the array
8
10
20
4
2
The large number is 20

Explaining the logic to find the largest

As said earlier the logic is dead simple. Just have a look at it.

int large=a[start]; Let us consider that the largest element in the array is present at the start position. start is the index and a[start] is the value in the array a at the start index. This value is stored in large variable.

for(int i=start+1;i<end;i++): Loop till the end right from the start+1 because we've already got the element at the start index now we will have to see from the next index.

if(a[i]>large) If the element at the index i is found to be large than the largest element we've found so far then the element at the index i in the array a is considered to be the largest. So this condition is satisfied whenever a[i] becomes larger than the large. The value in the variable large is updated every time we encounter a larger element than the previous value in the large variable. That's it.

Consider the output,
First we've considered the largest element to be 8, so large=8 at first.
When it comes to the loop, the value of i will be 1 because 0+1 is 1 and a[1]=10 which is large than 8. So the large will be 10.
Next, time the value of i becomes 2 (1+1) and a[2] =20 which is large than 10. So large=20 now.
Next, the value of i becomes 3(2+1) and a[3]=4 which is not large than 20. So large!=4
Next, the value of i becomes 4(3+1) and a[4]=2 which is not large than 20. So large!=2.
So at the end of the loop the value of large is 20. So now return that number.

Also see sorting an array in increasing order
In this way we can find largest number in array. The logic works fine, for any further improvements and clarifications feel free to drop a comment.


Posted by at on

Tags: Arrays,