Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Set and Multiset

Set

Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets:

  • Declaration

    set<int>s; //Creates a set of integers.
  • Size:

    int length=s.size(); //Gives the size of the set.
  • insert:

    s.insert(x); //Inserts an integer x into the set s.
  • Erasing an element:

    s.erase(val); //Erases an integer val from the set s.
  • Finding an element:

    set::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() .  
    Ex: set::iterator itr=s.find(100); //If 100 is not present then it==s.end().
  • Iterating through set
    for(set::iterator itr = s.begin(); itr != s.end(); itr++){
    cout<<*itr<<endl; // prints the element in the set
    }

Multiset

Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.

multiset<int> ms;

Removing Element from multiset which have a value multiple times

ms.erase(val) – Remove all instance of val from multiset
ms.erase(ms.find(val)) – Remove only one instance of val from multiset

Example

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    multiset<int> ms;
    ms.insert(10);
    ms.insert(10);
    ms.insert(10);
 
    // it will give output 3
    cout << ms.count(10) << endl;
 
    // removing single instance from multiset
   
    // it will remove only one value of 10 from multiset
    ms.erase(a.find(10));
    
    // it will give output 2
    cout << ms.count(10) << endl;
 
    // removing all instance of element from multiset it will remove all instance of value 10
    ms.erase(10);
   
    // it will give output 0 because all instance of value is removed from mulitset
    cout << a.count(10) << endl;
    return 0;
}

All the member function of the multisets are same as sets.

Try the following example in the editor below.

You will be given Q queries. Each query is 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).

Alfter processing all queries, if the size of set is greater than 0, print each element (sorted order) in the new line.

Input Format:

First line of the input contains an integer Q denoting the number of queries.
Next Q lines consists of two integers y and x where y is the type of the query and x is an integer.

Constraints

1 <= Q <= 105    
1 <= y <= 3
1 <= x <= 109

Sample Input

7  
1 5
1 4
1 6
1 5
3 4
2 4
3 4

Sample Output

Yes
No 5
6
Start solving Set and Multiset on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test