forked from Teju-1212/hacktoberfest-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind3largest.java
More file actions
23 lines (23 loc) · 771 Bytes
/
find3largest.java
File metadata and controls
23 lines (23 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class New {
private static int findThirdLargest(int[] array) {
int first, second, third;
first = second = third = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > first) {
third = second;
second = first;
first = array[i];
} else if (array[i] > second) {
third = second;
second = array[i];
} else if (array[i] > third) {
third = array[i];
}
}
return third;
}
public static void main(String[] args) {
int a[] = {4, 3, 2, 11, 23, 3, 44, 8, 93, 2, 34, 7, 8, 9};
System.out.println("Third largest number is " + findThirdLargest(a));
}
}