Largest number in array Java
In this post, you will learn how to find the largest number in an array using the Java programming language.
An array is a data structure representing an ordered collection of indexed items. An array is used when there is a requirement to add more items in a single variable. In the development process, we may come to the situation where we need to find the largest number in an array. Java provides various approaches to achieve this task. Here, we have mentioned most of them.
Find the largest number in array using temporary variable
Here, we iterate over the length of the array and compare two array elements, storing the largest in the temporary variable. Finally, we find the largest array element.
public class LargestInArray{
public static int largestNum(int[] arr, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr[total-1];
}
public static void main(String args[]){
int arr1[]={8,9,3,6,2,5};
int arr2[]={88,23,53,12,94,79,53};
System.out.println("Largest Number: "+largestNum(arr1,6));
System.out.println("Largest Number: "+largestNum(arr2,7));
}
}
Output of the above code:
Largest Number: 9
Largest Number: 94
Find the largest number in array using array sort
In the given program, we define a function to find the largest number and pass the array and its length as parameters. After that, we use the sort() method of the java.util.Arrays class and pass the array to be sorted. Once the array is sorted, we get the last element as the largest element.
import java.util.Arrays;
public class LargestInArray{
public static int findLargestNum(int[] arr, int len){
Arrays.sort(arr);
return arr[len-1];
}
public static void main(String args[]){
int a[]={89,34,66,22,81,64};
int b[]={90,54,22,67,45,24,79};
System.out.println("Largest Number: "+findLargestNum(a,6));
System.out.println("Largest Number: "+findLargestNum(b,7));
}
}
Output of the above code:
Largest Number: 89
Largest Number: 90
Find the largest number in array using for loop
Here, we have taken an array 'arr', which contains integer values. Initially, we assigned the first element of the array to the max variable and stored it. Next, within the for loop, we compared the ith element, i.e., the current element of 'arr' with the 'max' variable. If the value of the current element is greater than the value of the 'max', then we assign the value of the current element to the 'max' and the current index to the 'i'. At the end of the loop, we get the index of the maximum value from the array.
public class LargestElement {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {78,3,54,75,24,53,85};
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max)
max = arr[i];
}
System.out.println("Largest element: " + max);
}
}
Output of the above code:
Largest element: 85
Find the largest number in array using Collections
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in the java.util package. It consists entirely of static methods which are used to operate on collections. In the given Java program, we have used the collection to find the largest element in an array.
import java.util.*;
public class LargestElementInArray{
public static int findLargestNum(Integer[] arr, int total){
List list=Arrays.asList(arr);
Collections.sort(list);
int element=list.get(total-1);
return element;
}
public static void main(String args[]){
Integer arr1[]={8,4,2,7,9,5};
Integer arr2[]={89,43,64,23,68,45,38};
System.out.println("Largest Number: "+findLargestNum(arr1,6));
System.out.println("Largest Number: "+findLargestNum(arr2,7));
}
}
Output of the above code:
Largest Number: 9
Largest Number: 89
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java