229.多数元素II
方法:使用哈希表
class Solution {
public List<Integer> majorityElement(int[] nums) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0;i< nums.length;i++){
map.put(nums[i],map.getOrDefault(nums[i],0) + 1);
}
List<Integer> ans = new ArrayList<>();
for(int x : map.keySet()){
if(map.get(x) > nums.length / 3){
ans.add(x);
}
}
return ans;
}
}