logo CodeStepByStep logo

maxCount

Language/Type: C++ array lists arrays
Author: Marty Stepp (on 2016/06/16)

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: 
    your code goes here;
};
Type your C++ solution code here:


This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.


Log In

Need help?

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.

© Marty Stepp, all rights reserved.