C ++寻找数组中出现次数最多的元素 [英] C++ Looking for the Element with the highest occurrence in an array

查看:1303
本文介绍了C ++寻找数组中出现次数最多的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种优雅的方法来确定C ++ ptr数组中哪个元素的出现次数最多(模式).

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

例如,在

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

"apple"元素是最常用的元素.

the "apple" element is the most frequent one.

我以前的尝试失败了 数组已被排序.

My previous attempts have failed 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; 
}

推荐答案

对于该问题,有几种解决方案,但首先要提一些建议: 不要使用C样式的数组.将std::array用于固定(编译时)大小的数组,或将std::vector用于堆上的数组(或C ++ 14的std::dynarray,如果该数组的大小是在运行时确定的,但在创建后不会更改).这些容器为您执行内存管理,并且您无需单独传递数组大小.除了使用容器外,更喜欢在适当的地方使用<algorithm>中的算法.如果您不了解容器和算法,请花一些时间来熟悉它们,这些时间很快就会得到回报.

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. 对数组进行排序,然后计算连续值的出现次数.这比跟踪已计算的值和未计算的值要容易得多.基本上,您只需要两个值-计数对:一对用于当前计数的值,一对用于到目前为止的最大计数.您只需要第五个变量:容器的迭代器.

  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.

如果您无法对数组进行排序或需要跟踪所有所有计数,请使用映射将值映射到它们在数组中的出现次数.如果您熟悉std::map,则操作非常简单.最后,搜索最大数量,即最大地图值:

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;

注意:这使用基于C ++ 11的范围和C ++ 14多态Lambda.显而易见,这里所做的是什么,因此可以针对编译器提供的C ++ 11/C ++ 14支持进行调整.

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.

这篇关于C ++寻找数组中出现次数最多的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆