-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSecondMax.java
More file actions
43 lines (28 loc) · 814 Bytes
/
Copy pathFindSecondMax.java
File metadata and controls
43 lines (28 loc) · 814 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package array;
import java.util.Arrays;
public class FindSecondMax {
public static void main(String[] args) {
int[]a= {12,67,34,90,89,3,90,12,23,345,345,6,55};
/*
* first logic
int fmax=a[0];
int smax=fmax;
for(int i=0;i<a.length;i++) {
if(a[i]>fmax) {
if(smax!=fmax) {
smax=fmax;
}
fmax=a[i];
}
}
System.out.println(smax);
*/
//second way to find second max from array
Arrays.sort(a, 0, a.length/2);
System.out.println(a[a.length/2]); //this is the way to find second max number from half an array
//third way ton find second max from array
int[] b=Arrays.stream(a).distinct().toArray();
System.out.println(b[b.length-2]);
System.out.println(Arrays.binarySearch(a, 6));;
}
}