题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
import java.util.HashMap; import java.util.Map; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { Map<Integer, Integer> map = new HashMap<>(); int ans = 0; for (int i : array) { map.put(i, map.getOrDefault(i, 0) + 1); if (map.get(i) > array.length / 2) { ans = i; break; } } return ans; } }