36
0
0

C++算法之《快速找出数组中元素重复次数》

2025-02-11
2025-02-11
//假如这里我有一个vector数组
vector<int> arr = [1,2,2,2,3,3,4];

unorder_map<int, int> um;
for(auto e : arr){
  um[e]++;
}

vector<pair<int, int>> tmp;
for(auto [e, cnt]:um){
  tmp.emplace_back(e,cnt);
}

// 倒序排序
sort(tmp.rbegin(), tmp.rend());  

Support

If this article helped you, feel free to share it or give support!