C++ STL | Set 8 (unordered set) | Practice | GeeksforGeeks

Implement different operations on an unordered set s .

Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of input contains an integer Q denoting the no of queries . Then in the next line are Q space separated queries .
A query can be of four types 
1. a x (inserts an element x to the unordered set s)
2. b x (erases an element x from the unordered set s)
3. c x (prints 1 if the element x is present in the set else print -1)
4. d    (prints the size of the unordered set s)

Output:
The output for each test case will  be space separated integers denoting the results of each query . 

Constraints:
1<=T<=100
1<=Q<=100

Example(To be used only for only expected output):
Input
2
5
a 1 a 2 a 3 b 2 d
4
a 1 a 5 d c 2
 
Output
2
2 -1

Explanation :
For the first test case
There are five queries. Queries are performed in this order
1. a 1     {inserts 1 to set now set has {1} }
2. a 2     {inserts 2 to set now set has {1,2} }
3. a 3     {inserts 3 to set now set has {1,2,3} }
4. b 2     {removes 2 from the set }
5. d        {prints the size of the unordered set ie 2}

For the second test case 
There are four queries. Queries are performed in this order
1. a 1    {inserts 1 to set now set has {1} }
2. a 5    {inserts 5 to set now set has {1,5} }
3. d       {prints the size of the set ie 2}
4. c 2    {since 2 is not present in the set prints -1}


Note:The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
deydhananjoy53 months ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
void insert(unordered_set<int> &s,int x);
void erase(unordered_set<int> &s,int x);int size(unordered_set<int> &s);int find(unordered_set<int> &s,int x);
int main() {// your code goes hereint t;cin>>t;while(t--){ unordered_set<int> s; int q; cin>>q; while(q--) {  char c;  cin>>c;  if(c=='a')  {   int x;   cin>>x;   insert(s,x);  }   if(c=='b')  {   int x;   cin>>x;   erase(s,x);  }  if(c=='c')  {   int x;   cin>>x;   cout<<find(s,x)<<" ";  }if(c=='d')                      cout<<size(s)<<" ";  }cout<<endl;}return 0;}// } Driver Code Ends
/* You are required to complete below methods */
/*inserts an element x to the unordered set s */void insert(unordered_set<int> &s,int x){//Your code heres.insert(x);}
/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){//Your code heres.erase(x);}
/*returns the size of the unordered set s */int size(unordered_set<int> &s){     //Your code here    return s.size();}
/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){    //Your code here    if(s.count(x)==0)    return -1;    if (s.count(x)==1)    return 1;} 
0
deydhananjoy5
This comment was deleted.
0
Pawan chaudhary11 months ago
Pawan chaudhary
/*inserts an element x to the unordered set s */void insert(unordered_set<int> &s,int x){s.insert(x);}/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){s.erase(x);}/*returns the size of the unordered set s */int size(unordered_set<int> &s){      return s.size();}/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){     if(s.find(x)!=s.end())     return 1;     else     return -1;}
+2
Arpita1 year ago
Arpita
void insert(unordered_set<int> &s,int x){//Your code heres.insert(x);}
/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){//Your code hereif(s.find(x)!=s.end())s.erase(x);}
/*returns the size of the unordered set s */int size(unordered_set<int> &s){      //Your code here      int n = s.size();      return n; }
/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){     //Your code here     if(s.find(x)!=s.end())     return 1;     else     return -1;}
0
Nishant_00732 years ago
Nishant_0073
Correct Answer.Correct AnswerExecution Time:0.01
/* You are required to complete below methods *//*inserts an element x to the unordered set s */void insert(unordered_set<int> &s,int x){//Your code heres.insert(x);}/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){//Your code heres.erase(x);}/*returns the size of the unordered set s */int size(unordered_set<int> &s){      //Your code here      return s.size();}/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){     //Your code here     if(s.find(x)!=s.end())     {        return 1;     }     else     {         return -1;     }}
0
Ramswarup Kulhary2 years ago
Ramswarup Kulhary
void insert(unordered_set<int> &s,int x){s.insert(x);}void erase(unordered_set<int> &s,int x){s.erase(x);}int size(unordered_set<int> &s){      return s.size();}int find(unordered_set<int> &s,int x){     unordered_set<int> :: iterator it = s.begin();     while(it!= s.end())     {         if(*it == x)            return 1;        it++;     }     return -1;}
0
AMAN JAIN2 years ago
AMAN JAIN
void insert(unordered_set<int> &s,int x){s.insert(x);}
/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){s.erase(x);}
/*returns the size of the unordered set s */int size(unordered_set<int> &s){      return s.size();}
/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){     if(s.find(x)!=s.end())     return 1;     else     return -1;}
Execution Time:0.01
0
Saurabh Singh2 years ago
Saurabh Singh
void insert(unordered_set<int> &s,int x){s.insert(x);}/*erases an element x from the unordered set s */void erase(unordered_set<int> &s,int x){s.erase(x);}/*returns the size of the unordered set s */int size(unordered_set<int> &s){      return s.size();}/*returns 1 if the element x is presentin unordered set s else returns -1 */int find(unordered_set<int> &s,int x){     //Your code here     auto it = s.find(x);     if(it!=s.end())     return 1;     return -1;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.