In this article, you will learn how to find the largest number in an Array in java. To understand this program, you should have the basic knowledge of an integer array and the looping concept.
Algorithm-1.
- Declare and Initialize an integer array with some elements.
- Sort the array in descending order.
- For sorting, write two loops (one inside the other), the first loop start from 0 to array length – 1, and the second loop start from i+1 to arr length.
- Inside the second loop, swap the value between arr[i] and arr[j] with a temporary variable, if arr[i] < arr[j].
- After the completion of loops, the 0th index of the array will be the largest number.
- Finally, print the largest number.
Based on the above algorithm, we will see two examples (Example#1 and Example#2).
Example #1. Find the largest number in the array using java
In this example, we declare and initialize an integer array then we sort them in descending order, and after that, we get the 0th index value as the largest number. Let’s see the java code below:
package com.javacodepoint.array;
public class LargestNumberInArray {
public static void main(String[] args) {
// Declare and Initialize an integer Array
int arr[] = { 5, 3, 8, 0, 15, -5, 12 };
// Sorting the array element in descending order
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Array 0th element will be the largest no.
int max = arr[0];
// Printing the largest number
System.out.println("The Largest Number in the Array: " + max);
}
}
OUTPUT:
The Largest Number in the Array: 15
Example #2. Find the largest number in an array java using Scanner
In this example, we declare and initialize the integer array from the user-given value using Scanner, and the rest we did like example-1. Let’s have a look at the java code for it below:
package com.javacodepoint.array;
import java.util.Scanner;
public class LargestNumberInArray2 {
public static void main(String[] args) {
// Creating scanner object for reading user inputs
Scanner sc = new Scanner(System.in);
// Read the size of the Array
System.out.println("Enter the size of the Array: ");
int n = sc.nextInt();
// Declare the integer Array of given size
int arr[] = new int[n];
// Reading the Array values
System.out.println("Enter " + n + " element(s) of the Array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Sorting the array element in descending order
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Array 0th element will be the largest no.
int large = arr[0];
// Printing the largest number
System.out.println("The Largest Number in Given Array: " + large);
}
}
OUTPUT:
Enter the size of the Array:
5
Enter 5 element(s) of the Array:
2
4
9
1
0
The Largest Number in Given Array: 9
Algorithm-2.
- Declare and Initialize an integer array with elements given by the user.
- Declare a largeNo variable and initialize it with the 0th index of the array element. eg: largeNo=arr[0].
- Now iterate the array from index 1 to array length.
- Inside the loop, assign the arr[i] value into largeNo variable, if it is greater.
- After the completion of loops, the largeNo variable will hold the largest number.
- Finally, print the largest number.
Based on the above algorithm 2, we will see Example#3 below:
Example #3. Find the largest number in the array without sorting
In this example, we will find the largest number in an array without using any sorting technique. Let’s see the java code below:
package com.javacodepoint.array;
import java.util.Scanner;
public class LargestNumberInArray3 {
public static void main(String[] args) {
// Creating scanner object for reading user inputs
Scanner sc = new Scanner(System.in);
// Read the size of the Array
System.out.println("Enter the size of the Array: ");
int n = sc.nextInt();
// Declare the integer Array of given size
int arr[] = new int[n];
// Reading the Array values
System.out.println("Enter " + n + " element(s) of the Array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Assume 0th element will be the largest no.
int largeNo = arr[0];
// Iterate the array from index 1
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largeNo) {
largeNo = arr[i];
}
}
// Printing the largest number
System.out.println("The Largest Number in Given Array: " + largeNo);
}
}
OUTPUT:
Enter the size of the Array:
6
Enter 6 element(s) of the Array:
2
10
-5
6
12
8
The Largest Number in Given Array: 12
Algorithm-3.
- Declare and Initialize an integer array with elements given by the user.
- Convert the given array into a list collection. eg: Arrays.asList(arr);
- Sort the list in ascending order. eg: Collections.sort(list);
- Get the array length – 1 index value from the list. eg: list.get(arr.length-1);
- The last (arr length -1) index value will be the largest number in the given array.
- Finally, print the largest number.
Based on the above algorithm 3, we will see the below Example #4:
Example #4. Find the large number using Collections in java
In this example, we use the collections to find the largest number in java. Let’s see the code below:
package com.javacodepoint.array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class LargestNumberInArray4 {
public static void main(String[] args) {
// Creating scanner object for reading user inputs
Scanner sc = new Scanner(System.in);
// Read the size of the Array
System.out.println("Enter the size of the Array: ");
int n = sc.nextInt();
// Declare the integer Array of given size
Integer arr[] = new Integer[n];
// Reading the Array values
System.out.println("Enter " + n + " element(s) of the Array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Convert int array into list collection
List<Integer> list = Arrays.asList(arr);
// Sort the list in ascending order
Collections.sort(list);
// The last element of the list will be largest no.
int largeNumber = list.get(arr.length - 1);
// Printing the largest number
System.out.println("The Largest Number in Given Array: " + largeNumber);
}
}
OUTPUT:
Enter the size of the Array:
4
Enter 4 element(s) of the Array:
2
8
0
16
The Largest Number in Given Array: 16
See also: Java Program to find the Smallest Number in an Array.