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
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# 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 > ();
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 > ();
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 `

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

Hints
• Complete Solution

## Discussion

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
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/University Name
Job Title
Job Title
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