forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shashank.java
28 lines (21 loc) · 829 Bytes
/
shashank.java
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
import java.util.Arrays;
public class LargestElementFinder {
public static int findLargestElement(int[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array is empty.");
}
int largest = arr[0]; // Assume the first element is the largest
// Iterate through the array to find the largest element
for (int num : arr) {
if (num > largest) {
largest = num; // Update largest if current element is greater
}
}
return largest; // Return the largest element found
}
public static void main(String[] args) {
int[] arr = {10, 5, 20, 8};
int largestElement = findLargestElement(arr);
System.out.println("The largest element in the array is: " + largestElement);
}
}