I was solving a simple problem of finding unique elements in an array. I used a std::unordered_map
to count unique elements but it gave Time Limit Exceeded in one test case. Then I used a std::unordered_set
with the same result.
PS: I used std::unordered_map
and std::unordered_set
because I read that these two are much much faster than std::map
and std::set
respectively.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, a;
cin >> n;
unordered_set<int> s;
for(int i = 0; i < n; i++) {
cin >> a;
s.insert(a);
}
cout << s.size();
}
测试7超过了时间限制。
我的问题是:
If std::unordered_map
and std::unordered_set
are faster why did they give TLE?