forked from ironhack-labs/lab-java-loops-and-version-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_two.java
More file actions
27 lines (24 loc) · 913 Bytes
/
Copy pathtask_two.java
File metadata and controls
27 lines (24 loc) · 913 Bytes
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
public class task_two {
public static void findSmallestElements(int[] arr) {
if (arr == null || arr.length == 0) {
System.out.println("Array is empty or null.");
return;
}
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int num : arr) {
if (num < smallest) {
secondSmallest = smallest;
smallest = num;
} else if (num < secondSmallest && num != smallest) {
secondSmallest = num;
}
}
System.out.println("Smallest element: " + smallest);
System.out.println("Second smallest element: " + (secondSmallest == Integer.MAX_VALUE ? "No second smallest element" : secondSmallest));
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 7};
findSmallestElements(arr);
}
}