Skip to content

Commit cf88e44

Browse files
authored
Update Find Second largest Element in the Array.cpp
1 parent c3ac18a commit cf88e44

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Array/Find Second largest Element in the Array.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
//optimized
2+
3+
class SecondLargest {
4+
5+
public static void main(String[] args) {
6+
int[] arr = new int[]{1,2,6,3,4};
7+
int first = Integer.MIN_VALUE;
8+
int second = Integer.MIN_VALUE;
9+
for (int value : arr) {
10+
if (value > first) {
11+
second = first;
12+
first = value;
13+
} else if (value > second && value != first) {
14+
second = value;
15+
}
16+
}
17+
System.out.println(second);
18+
}
19+
}
20+
21+
122
class Solution{
223
public:
324
// Function returns the second

0 commit comments

Comments
 (0)