0

I was solving a problem where I have to find the number of distinct numbers so I used unordered_set and insert the given elements in the set, then I printed the size of the set.


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    unordered_set<int> set;
    int temp;
    for(int i=0; i<n; i++)
    {
        cin>>temp;
        set.insert(temp);
    }
    cout<<set.size()<<endl;
    return 0;
}

But in a few test cases, it shows TLE. So I changed the unoreded_set to set and it worked.

I was searching on the internet how insertion works in set and unordered_set and how their time complexity varies in different operations but was not able to find satisfying answer.

  • _"TLE "_ means what exactly again?? I am not familiar with that acronym regarding standard c++ programming. That must come from some cargo cult, adopted elsewhere. – πάντα ῥεῖ Jan 11 '22 at 21:10
  • 2
    @OP -- Instead of abbreviations and acronyms such as "TLE", please understand that "competition programming" website keywords are not something that is known in C++. Please spell out what "TLE" means, so that others who know nothing about these websites you are using will know what you mean. – PaulMcKenzie Jan 11 '22 at 21:16

0 Answers0