-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain5.java
More file actions
35 lines (33 loc) · 986 Bytes
/
Main5.java
File metadata and controls
35 lines (33 loc) · 986 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
import java.util.*;
/**
* Created by LXF on 2017/8/14.
*/
public class Main5 {
private static int MoreThanHalfNum_Solution(int [] array) {
Map map = new HashMap<Integer, Integer>();
int max = 0;
int v = 0;
int flag = 0;
for (int i = 0; i < array.length; i++) {
if (!map.containsKey(array[i])) {
map.put(array[i], 1);
} else {
int newVal = (Integer) map.get(array[i]) + 1;
max = max > newVal ? max : newVal;
v = array[i];
map.put(array[i], newVal);
flag =1;
}
}
if (flag == 0) {
max = 1;
v = array[0];
}
return max>array.length/2 ? v :0;
}
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
int[] a = new int[]{1,2,3,2,2,2,5,4,2};
System.out.println(MoreThanHalfNum_Solution(a));
}
}