logo Practice-It logo


Related Links:
Author: Stuart Reges (on 2014/02/13)

Write a method maxCount that returns the number of occurrences of the most frequently occuring 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). Thefore, the following call:


should return 4 to indicate that the most frequently occuring 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.

You are writing a method for the ArrayIntList class discussed in lecture (handouts 3, 5 and 6):

    public class ArrayIntList {
        private int[] elementData; // list of integers
        private int size;          // current # of elements in the list


You are not to call any other ArrayIntList methods to solve this problem. Your solution must run in O(n) time. You may not use any auxiliary data structures to solve this problem, although you can have as many simple variables as you like. Remember that you can assume that the values appear in sorted (nondecreasing) order.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.