考虑使用map维护每个数出现的次数,发现可以顺便维护下map中最大的值 maxn ,答案就是 min(maxn + 1, n)
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5; int __t = 1, n, x; void solve() { map<int, int> mp; cin >> n; int maxn = 0; for (int i = 1; i <= n; i++) { cin >> x; mp[x]++; maxn = max(maxn, mp[x]); } cout << min(maxn + 1, n) << '\n'; return; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }