Write a method maxCount
that returns the number of occurrences of the most frequently occurring value in a sorted list of integers. Because the list will be sorted, all duplicates will be grouped together, which will make it easier to count duplicates. For example, suppose that a variable called list
stores the following sequence of values:
[1, 3, 4, 7, 7, 7, 7, 9, 9, 11, 13, 14, 14, 14, 16, 16, 18, 19, 19, 19]
This list has values that occur just once (1, 3, 4, 11, 13, 18), values that occur twice (9, 16), values that occur three times (14, 19) and a single value that occurs four times (7). Therefore, the call of list.maxCount()
should return 4 to indicate that the most frequently occurring value occurs 4 times. It is possible that there will be a tie for the most frequently occurring value, but that doesn't affect the outcome because you are just returning the count, not the value. For example, if there are no duplicates in the list, then every value will occur exactly once and the maximum would be 1. If the list is empty, your method should return 0.
Assume you are adding to the ArrayIntList
class with following fields:
public class ArrayIntList {
private int[] elementData;
private int size;
// your code goes here
}