迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中数组中查找出现最频繁的元素

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将演示有关如何在数组 C++ 中查找出现最频繁的元素的多种方法。

使用 std::sort 算法与迭代法来寻找数组中最频繁的元素

在数组中查找最频繁的元素的简单方法是遍历数组的排序版本并保留元素频率的计数。在这种情况下,我们假设数组是整数序列,它们存储在 std::vector 容器中。

首先,我们需要使用 std::sort 算法对整数数组进行排序,使一次遍历足以找到最频繁的元素。注意,在迭代过程中我们需要几个变量。即,我们存储最后一次迭代中的整数以与当前元素进行比较;同样,我们在循环的每个循环中都更新最频繁的整数的值。该算法检查当前元素是否等于前一个元素,并在表达式为真时递增频率计数器。如果不为真,则检查当前频率计数是否大于到目前为止遇到的最大值,如果是,则存储最大频率计数和最频繁元素的更新值。

然后,我们修改之前的整数变量,并将当前频率重置为 1。循环结束后,还有一个 if 条件可以比较当前频率和最大频率,然后我们可以确定结果元素。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::vector; using std::sort;
using std::endl;

int getMostFrequentElement(vector<int> &arr)
{
    if (arr.empty())
        return -1;

    sort(arr.begin(), arr.end());

    auto last_int = arr.front();
    auto most_freq_int = arr.front();
    int max_freq = 0, current_freq = 0;

    for (const auto &i : arr) {
        if (i == last_int)
            ++current_freq;
        else {
            if (current_freq > max_freq) {
                max_freq = current_freq;
                most_freq_int = last_int;
            }

            last_int = i;
            current_freq = 1;
        }
    }

    if (current_freq > max_freq) {
        max_freq = current_freq;
        most_freq_int = last_int;
    }

    return most_freq_int;
}

int main(){
    vector<int> arr = { 1,2,3,4,5,6,7,8,9,10,
                        2,3,4,5,6,7,8,9,10,10,
                        2,3,4,5,6,7,8,9,10,10 };

    int ret = getMostFrequentElement(arr);
    if (ret == -1) {
        perror("getMostFrequentElement");
        exit(EXIT_FAILURE);
    }
    cout << "Most frequent element = " << ret << endl;

    exit(EXIT_SUCCESS);
}

输出:

Most frequent element = 10

std::unordered_map 容器与 std::max_element 函数一起使用以查找数组中最频繁的元素

另外,我们可以利用 std::unordered_map 类为每个元素累积频率,然后调用 std::max_element 算法来识别具有最大值的元素。请注意,累积频率需要遍历整个数组,并且每次迭代都需要在映射中插入。在这种情况下,std::max_element 方法采用三个参数,前两个参数:范围的开始和结束说明符。第三个参数是 lambda 函数,用于比较 std::unordered_map 类型为 std::pair 的元素。最后,我们可以从返回的 max_element 对算法中返回第二项。

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

using std::cout; using std::cin;
using std::endl; using std::unordered_map;
using std::vector; using std::sort;

int getMostFrequentElement(vector<int> &arr)
{
    if (arr.empty())
        return -1;

    unordered_map<int, int> freq_count;

    for (const auto &item : arr)
        freq_count[item]++;


    auto most_freq_int =
            std::max_element(freq_count.begin(), freq_count.end(),
                 [] (const auto &x, const auto &y) {return x.second < y.second;});

    return most_freq_int->first;
}

int main(){
    vector<int> arr = { 1,2,3,4,5,6,7,8,9,10,
                        2,3,4,5,6,7,8,9,10,10,
                        2,3,4,5,6,7,8,9,10,10 };

    int ret = getMostFrequentElement(arr);
    if (ret == -1) {
        perror("getMostFrequentElement");
        exit(EXIT_FAILURE);
    }
    cout << "Most frequent element = " << ret << endl;

    exit(EXIT_SUCCESS);
}

输出:

Most frequent element = 10

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便