logo CodeStepByStep logo

maxCount

Language/Type: C++ array lists arrays

Write a member function named maxCount that could be added to the ArrayIntList class. Your function should examine the elements of the list and return the number of occurrences of the most frequently occurring value in the list of integers. For this problem, you should assume that the elements in the list are in sorted order. Because the list will be sorted, all duplicates will be grouped together, which will make it easier to count duplicates. For example, suppose that an ArrayIntList variable named 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 some values that occur just once (1, 3, 4, 11, 13, 18), some values that occur twice (9, 16), some values that occur three times (14, 19) and a single value that occurs four times (7). Therefore, the client's 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. 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, you should return 0.

Constraints: You may call other member functions of the ArrayIntList if you like, but your function should not modify the state of the list. Its header should be declared in such a way as to indicate to the client that this function does not modify the list. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Write the member function as it would appear in ArrayIntList.cpp. You do not need to declare the function header that would appear in ArrayIntList.h. Assume that you are adding this method to the ArrayIntList class as defined below:

class ArrayIntList {
private:
    int* elements;   // array storing element data
    int mysize;      // number of elements in the array
    int capacity;    // array's length

public:
    ...
};
Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.