forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT28.java
More file actions
35 lines (33 loc) · 1.08 KB
/
T28.java
File metadata and controls
35 lines (33 loc) · 1.08 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
package web; /**
* @program LeetNiu
* @description: 数组中出现次数超过一半的数字
* @author: mf
* @create: 2020/01/14 00:19
*/
import java.util.HashMap;
import java.util.Map;
/**
* 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
* 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。
* 由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
*/
public class T28 {
public int MoreThanHalfNum_Solution(int [] array) {
// 哈希
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
map.put(array[i], map.get(array[i]) + 1);
} else {
map.put(array[i], 1);
}
}
int length = array.length >> 1;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > length) {
return entry.getKey();
}
}
return 0;
}
}