C++ Looking for the Element with the highest occurence in an array

落花浮王杯 提交于 2019-12-30 14:37:13

问题


I'm looking for an elegant way of determining which element has the highest occurrence (mode) in a C++ ptr array.

For example, in

{"pear", "apple", "orange", "apple"}

the "apple" element is the most frequent one.

My previous attempts have failed EDIT: The array has already been sorted.

int getMode(int *students,int size)
{
    int mode;
    int count=0, 
    maxCount=0,
    preVal;

    preVal=students[0]; //preVall holds current mode number being compared
    count=1;
    for(int i =0; i<size; i++) //Check each number in the array
    {
        if(students[i]==preVal) //checks if current mode is seen again
        {
            count++; //The amount of times current mode number has been seen.
            if(maxCount<count)  //if the amount of times mode has been seen is more than maxcount
            {
                maxCount=count; //the larger it mode that has been seen is now the maxCount
                mode=students[i]; //The current array item will become the mode
            }else{
                preVal = students[i];
                count = 1;
            }

        }

    }

    return mode; 
}

回答1:


There are several possible solutions to that problem, but first some advice: Don't use C-style arrays. Use std::array for fixed (compiletime) size arrays or std::vector for arrays on the heap (or C++14's std::dynarray if the array size is determined at runtime but does not change after creation). Those containers do the memory management for you, and you do not need to pass the array size around separately. In addition to using containers, prefer to use the algorithms in <algorithm> where appropiate. If you don't know the containers and algorithms, take some time to get familiar with them, that time will pay off very soon.

So, here are some solution sketches:

  1. Sort the array, then count the ocurrences of consecutive values. It's much easier than to keep track of which values you have already counted and which not. You basically need only two value-count pairs: one for the value you are currently counting, one for the maximum count up to now. You will only need a fifth variable: the iterator for the container.

  2. If you cannot sort your array or need to keep track of all counts, use a map to map values to their number of occurrence in the array. If you are familiar with std::map, that is very simple to do. At the end, search for the maximum count, i.e. for the maximum map value:

    for (auto i: students) countMap[i]++;
    auto pos = std::max_element(begin(countMap), end(countMap), 
      [](auto lhs, auto rhs){ return lhs.second < rhs.second }); //! see below
    auto maxCount = pos->second;
    

Note: this uses C++11's range based for and a C++14 polymorphic Lambda. It should be obvious what is done here, so it can be adjusted for the C++11/C++14 support your compiler provides.



来源:https://stackoverflow.com/questions/16412296/c-looking-for-the-element-with-the-highest-occurence-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!