-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondLargest.java
More file actions
45 lines (40 loc) · 1.16 KB
/
Copy pathSecondLargest.java
File metadata and controls
45 lines (40 loc) · 1.16 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.programs;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class SecondLargest {
public static void main(String args[]) {
// Method 1
int[] a = { 1, 2, 5, 6, 3, 2 };
System.out.println("length of array is " + a.length);
int total = a.length;
for (int i = 0; i < total; i++) {
for (int j = i + 1; j < total; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("array after sorting");
for (int i = 0; i < total; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("Second largest no. in an array is " + a[total - 2]);
// Method 2
int[] b = { 1, 2, 5, 6, 7, 3, 2 };
Arrays.sort(b);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
System.out.println();
System.out.println("Second largest no. in an array is " + b[b.length - 2]);
// Method 3
Integer[] c = { 1, 3, 5, 6, 4 };
List<Integer> list = Arrays.asList(c);
Collections.sort(list);
System.out.println("Second largest no. in an array is " + list.get(c.length - 2));
}
}