题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if(array == null || array.length == 0){
return -1;
}
return Arrays.stream(array)
.boxed()
.collect(Collectors.toMap(e->e,e->1,Integer::sum))
.entrySet()
.stream()
.filter(e->e.getValue() > array.length/2)
.collect(Collectors.toList())
.get(0)
.getKey();
}
} 

联想公司福利 1500人发布