I rewrite the remove part to make it simpler.

public class Solution {
public List<Integer> majorityElement(int[] nums) {
if(nums == null || nums.length == 0) return new ArrayList<>();
return helper(nums, 3);
}
public List<Integer> helper(int[] nums, int k){
Map<Integer, Integer> majorities = new HashMap<>();
List<Integer> ret = new ArrayList<>();
for(int num : nums){
if(majorities.containsKey(num)) majorities.put(num, majorities.get(num) + 1);
else if(majorities.keySet().size() < k - 1) majorities.put(num, 1);
else{
Iterator<Map.Entry<Integer, Integer>> ite = majorities.entrySet().iterator();
while(ite.hasNext()){
Map.Entry<Integer, Integer> entry = ite.next();
int val = entry.getValue();
if(val == 1) ite.remove();
else entry.setValue(val - 1);
}
}
}
for(Integer i : majorities.keySet()){
majorities.put(i, 0);
}
for(int num : nums){
if(majorities.containsKey(num)) majorities.put(num, majorities.get(num) + 1);
}
int target = nums.length / k;
for(Integer i : majorities.keySet()){
if(majorities.get(i) > target) ret.add(i);
}
return ret;
}
}