已知int arr9 = 261223613;统计该数组中个数据重复次数
可以使用一个map来统计数组中每个元素的重复次数,最后遍历map输出结果。
#include <iostream>
#include <map>
int main() {
int arr[9] = {2, 6, 1, 2, 2, 3, 6, 1, 3};
std::map<int, int> countMap;
for (int i = 0; i < 9; i++) {
countMap[arr[i]]++;
}
for (auto it = countMap.begin(); it != countMap.end(); it++) {
std::cout << "元素 " << it->first << " 重复次数:" << it->second << std::endl;
}
return 0;
}
输出结果为:
元素 1 重复次数:2
元素 2 重复次数:3
元素 3 重复次数:2
元素 6 重复次数:2
原文地址: http://www.cveoy.top/t/topic/ib9d 著作权归作者所有。请勿转载和采集!