
这里我们会看到一个问题。我们有一个数组。我们的任务是找到那些频率大于 1 的元素。假设元素是 {1, 5, 2, 5, 3, 1, 5, 2, 7}。这里1出现了2次,5出现了3次,2出现了3次,其他只出现了一次。因此输出将是 {1, 5, 2}
Begin
define map with int type key and int type value
for each element e in arr, do
increase map.key(arr).value
done
for each key check whether the value is more than 1, then print the key
End#include <iostream>
#include <map>
using namespace std;
void moreFreq(int arr[], int n){
map<int, int> freq_map;
for(int i = 0; i<n; i++){
freq_map[arr[i]]++; //increase the frequency
}
for (auto it = freq_map.begin(); it != freq_map.end(); it++) {
if (it->second > 1)
cout << it->first << " ";
}
}
int main() {
int arr[] = {1, 5, 2, 5, 3, 1, 5, 2, 7};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Frequency more than one: ";
moreFreq(arr, n);
}Frequency more than one: 1 2 5
以上就是出现多次的数组元素?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号