public class Solution {
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
*/
public int majorityNumber(ArrayList<Integer> nums, int k) {
HashMap<Integer, Integer> counters = new HashMap<Integer, Integer>();
for (Integer i : nums) {
if (!counters.containsKey(i)) {
counters.put(i, 1);
} else {
counters.put(i, counters.get(i) + 1);
}
if (counters.size() >= k) {
removeKey(counters);
}
}
// corner cases
if (counters.size() == 0) {
return Integer.MIN_VALUE;
}
// recalculate counters
for (Integer i : counters.keySet()) {
counters.put(i, 0);
}
for (Integer i : nums) {
if (counters.containsKey(i)) {
counters.put(i, counters.get(i) + 1);
}
}
// find the max key
int maxCounter = 0, maxKey = 0;
for (Integer i : counters.keySet()) {
if (counters.get(i) > maxCounter) {
maxCounter = counters.get(i);
maxKey = i;
}
}
return maxKey;
}
private void removeKey(HashMap<Integer, Integer> counters) {
Set<Integer> keySet = counters.keySet();
List<Integer> removeList = new ArrayList<Integer>();
for (Integer key : keySet) {
counters.put(key, counters.get(key) - 1);
if (counters.get(key) == 0) {
removeList.add(key);
}
}
for (Integer key : removeList) {
counters.remove(key);
}
}
}