Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Sets in C#

In this article, we will learn about the sets in C#.

A set is a collection that contains no duplicate elements.

There are two types of sets in C# under the System.Collections.Generic namespace.
<ul><li>HashSet</li><li>SortedSet</li></ul>

HashSet does not store the elements in order while giving faster operation time.
SortedSet stores the elements in order which allows more functionality.

Creating a Set

We create a HashSet / SortedSet in C# using the following syntax:
HashSet<T> l1 = new HashSet<T>();
SortedSet<T> l1 = new SortedSet<T>();
<T> here denotes any datatype of which we want to create a list of.


Properties of Sets

  • Count: This property gets the number of elements actually contained in the set.
  • Comparer: Determines equality for the values in the set.
  • Max: Gets the maximum value in the set, as defined by the comparer. (only for SortedSet)
  • Min: Gets the maximum value in the set, as defined by the comparer. (only for SortedSet)

Methods of Sets

  • Add(T): Adds the element to the set.
  • Clear(): Removes all the elements from the set.
  • Contains(T): Determines whether a value is in the set.
  • Remove(T): Removes the element from the set.

Set Operation Examples:

Different Set operations are demonstrated in the example below:

using System;
using System.Collections.Generic;
namespace SetDemo {
    class Example {
        static void Main(string[] args) {
            HashSet < int > HS = new HashSet < int > ();
            HS.Add(5);          
            HS.Add(5);          
            HS.Add(2);
            HS.Add(9);
            Console.WriteLine(HS.Count);        // Prints 3 because 5 is only added once in the set
            
            Console.WriteLine(HS.Contains(5));  // Checks if 5 is present
            
            HS.Remove(5);                       // Removes 5
            
            Console.WriteLine(HS.Contains(5));  // Prints False this time
            
            SortedSet < int > SS = new SortedSet < int > ();
            SS.Add(5);          
            SS.Add(5);          
            SS.Add(2);
            SS.Add(9);
            Console.WriteLine(SS.Max);   // Prints the maximum element in the set
            
            Console.WriteLine(SS.Min);   // Prints the minimum element in the set
            foreach(int i in SS) {
                Console.Write(i + " ");
            }
            Console.WriteLine();
        }
    }
}

Output:

3

True

False

9

2

2 5 9


Task

In the editor below, perform the different tasks as directed.

Start solving Sets in C# 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.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
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
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
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
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
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