Open YuezhenQin opened 11 months ago
Anytime you need to count anything, think about using a hash map to do it.
map.put(map.getOrDefault(key, 0) + 1)
Map.Entry<Integer, Integer> maxEntry = null;
for (Map.Entry<Integer, Integer> entry : map.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
A
Map
is an object that maps keys to values, or is a collection of key-value pairs. It models the function abstraction in mathematics.