Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Sets-STL solution in c++ programming

YASH PAL, 31 July 202422 August 2024

In this HackerRank Sets-STL problem in the c++ programming language, you will be given Q queries. Each query is of one of the following three types:

  1. x: Add an element x to the set.
  2. x: Delete an element x from the set. (If the number x is not present in the set, then do nothing).
  3. x: If the number x is present in the set, then print “Yes”(without quotes) else print “No”(without quotes).
HackerRank Sets-STL solution in c++ programming

HackerRank Sets-STL problem solution in c++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;


int main() {
int iCount;
set<int> ss;
cin >> iCount;
for (int i=0; i<iCount; ++i){
    int type, query;
    cin >> type >> query;
    switch (type){
        case 1:
            ss.insert(query);
            break;
        case 2:
            ss.erase(query);
            break;
        case 3:
            cout << (ss.find(query) == ss.end() ? "No" : "Yes") << endl;
            break;
    }
}  
return 0;
}


Second solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;


int main() {
    int c,t,n;
    scanf("%d",&n);
    set<int> ns;
    while(n--) {
        scanf("%d %d",&c,&t);
        switch(c) {
            case 1:
                ns.insert(t);
                break;
            case 2:
                ns.erase(t);
                break;
            case 3:
                if (ns.find(t)!=ns.end())
                    cout << "Yes"<<endl;
                else
                    cout <<"No"<<endl;
                break;
            default:
                cout<<"invalid switch value: "<<c<<endl;
                
        }
    }
    
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}
coding problems cpp hackerrank solutions

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes