Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. HashSet<T>

HashSet<T>

HashSet<T> is a collection that automatically eliminates duplicates. It excels at set operations (union, intersection, difference) and can perform fast duplicate checks on large datasets.

Syntax

// Create a HashSet.
HashSet<T> set = new HashSet<T>();

// Create from an existing collection (duplicates are removed automatically).
HashSet<T> set = new HashSet<T>(collection);

Method List

Method / PropertyDescription
Add(item)Adds an element. Returns false if the element already exists.
Remove(item)Removes the specified element. Returns true on success.
Contains(item)Returns a bool indicating whether the element exists. Faster than List.
CountReturns the number of elements.
UnionWith(other)Modifies the set to contain all elements from either collection (union).
IntersectWith(other)Modifies the set to contain only elements present in both collections (intersection).
ExceptWith(other)Modifies the set to remove all elements found in other (difference).
IsSubsetOf(other)Returns whether the current set is a subset of other.

Sample Code

using System;
using System.Collections.Generic;

// Duplicates are removed automatically.
HashSet<int> set = new HashSet<int> { 1, 2, 3, 2, 1 };
Console.WriteLine(set.Count); // 3 (duplicates removed)

// Remove duplicates from an existing list.
List<string> tagList = new List<string> { "C#", "C++", "C#", "Python", "C++" };
HashSet<string> uniqueTags = new HashSet<string>(tagList);
foreach (string tag in uniqueTags) {
	Console.Write(tag + " "); // C# C++ Python (order is not guaranteed)
}
Console.WriteLine();

// Union (UnionWith): elements in either set.
HashSet<int> A = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> B = new HashSet<int> { 3, 4, 5, 6 };
A.UnionWith(B);
Console.WriteLine(string.Join(", ", A)); // 1, 2, 3, 4, 5, 6

// Intersection (IntersectWith): elements present in both sets.
HashSet<int> C = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> D = new HashSet<int> { 3, 4, 5, 6 };
C.IntersectWith(D);
Console.WriteLine(string.Join(", ", C)); // 3, 4

// Difference (ExceptWith): elements in C but not in D.
HashSet<int> E = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> F = new HashSet<int> { 3, 4, 5, 6 };
E.ExceptWith(F);
Console.WriteLine(string.Join(", ", E)); // 1, 2

Notes

HashSet<T> does not guarantee element order, but lookups via Contains() are extremely fast. It is especially effective for removing duplicates from large datasets or checking whether an item has already been processed.

UnionWith(), IntersectWith(), and ExceptWith() modify the instance they are called on. If you need to preserve the original set, create a copy beforehand.

For ordered collections, see 'IEnumerable<T> / IList<T>'.

If you find any errors or copyright issues, please .