Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Check if the Hashtable contains a specific Value
Next article icon

C# | Check if a HashSet contains the specified element

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet.Contains(T) Method is used to check whether a HashSet object contains the specified element. Syntax:
  mySet.Contains(T item);  
Here, mySet is the name of the HashSet and item is the required element to locate in the HashSet object. Return Type: This method returns true if the HashSet object contains the specified element; otherwise, false. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP
// C# code to check if a HashSet // contains the specified element using System; using System.Collections.Generic;  class GFG {      // Driver code     public static void Main()     {          // Creating a HashSet of strings         HashSet<string> mySet = new HashSet<string>();          // Inserting elements in HashSet         mySet.Add("DS");         mySet.Add("C++");         mySet.Add("Java");         mySet.Add("JavaScript");          // Check if a HashSet contains         // the specified element         if (mySet.Contains("Java"))             Console.WriteLine("Required Element is present");         else             Console.WriteLine("Required Element is not present");     } } 
Output:
  Required Element is present  
Example 2: CSHARP
// C# code to check if a HashSet // contains the specified element using System; using System.Collections.Generic;  class GFG {      // Driver code     public static void Main()     {          // Creating a HashSet of integers         HashSet<int> mySet = new HashSet<int>();          // Inserting elements in HashSet         for (int i = 0; i < 5; i++) {             mySet.Add(i * 2);         }          // Check if a HashSet contains         // the specified element         if (mySet.Contains(5))             Console.WriteLine("Required Element is present");         else             Console.WriteLine("Required Element is not present");     } } 
Output:
  Required Element is not present  
Reference:
  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.contains?view=netframework-4.7.2

Next Article
C# | Check if the Hashtable contains a specific Value

S

Sahil_Bansall
Improve
Article Tags :
  • Misc
  • C#
  • CSharp-method
  • CSharp-Generic-HashSet
  • CSharp-Generic-Namespace
Practice Tags :
  • Misc

Similar Reads

  • C# | Check if the SortedSet contains a specific element
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov
    2 min read
  • C# | Check if HashSet and the specified collection contain the same elements
    Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C/C++ Code using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet
    3 min read
  • C# | Check if the Hashtable contains a specific Key
    The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public v
    2 min read
  • C# | Check if the Hashtable contains a specific Value
    The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: publ
    2 min read
  • C# | Check if a HashSet and a specified collection share common elements
    In C#, you can use the SetEquals method to check if a HashSet and a specified collection contain the same elements. The SetEquals method returns true if the HashSet and the specified collection contain the same elements; otherwise, it returns false. Here is an example code that demonstrates how to u
    3 min read
  • C# | Check if a HashSet is a subset of the specified collection
    A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet.I
    2 min read
  • C# | Check if a Stack contains an element
    Stack represents a last-in, first out collection of object. Stack<T>.Contains(Object) Method is used to check whether an element is in the Stack<T> or not. Syntax: public virtual bool Contains(object obj); Return Value: The function returns True if the element exists in the Stack<T
    2 min read
  • C# | Check if a HashSet is a superset of the specified collection
    Sure, here's an example code that demonstrates using the IsSupersetOf method to check if a HashSet<string> is a superset of a List<string> and then printing out a message indicating the result: C/C++ Code using System; using System.Collections.Generic; public class Program { public stati
    3 min read
  • C# | Remove the specified element from a HashSet
    A HashSet is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet
    3 min read
  • C# | Check if SortedSet and the specified collection contain the same elements
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.SetEquals(IEnumerable) Method is used to check whether the SortedSet and the specified collection contain the same elements. Properties: In C#, SortedSet c
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences