Program to find largest element in an array
The solution is to initialize max as the first element, then traverse the given array from the second element till the end. For every traversed element, compare it with max, if it is greater than max, then update max.
// Java Program to find maximum in arr[]
class Test
{
static int arr[] = {10, 324, 45, 90, 9808};
// Method to find maximum in arr[]
static int largest()
{
int i;
// Initialize maximum element
int max = arr[0];
// Traverse array elements from second and
// compare every element with current max
for (i = 1; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Driver method
public static void main(String[] args)
{
System.out.println("Largest in given array is " + largest());
}
}