C# tutorials > Core C# Fundamentals > Data Structures and Collections > What are the different implementations of the `Set<T>` interface (`HashSet<T>`, `SortedSet<T>`)?
What are the different implementations of the `Set<T>` interface (`HashSet<T>`, `SortedSet<T>`)?
Sets in C# provide a way to store unique elements. The `Set
Introduction to `HashSet` and `SortedSet`
In C#, both `HashSet
Code Example: Basic Usage
This code demonstrates the basic usage of `HashSet
using System;
using System.Collections.Generic;
public class SetExample
{
public static void Main(string[] args)
{
// HashSet<T> example
HashSet<int> hashSet = new HashSet<int>();
hashSet.Add(5);
hashSet.Add(2);
hashSet.Add(8);
hashSet.Add(2); // Duplicate, won't be added
Console.WriteLine("HashSet: " + string.Join(", ", hashSet)); // Output: HashSet: 5, 2, 8 (order may vary)
// SortedSet<T> example
SortedSet<int> sortedSet = new SortedSet<int>();
sortedSet.Add(5);
sortedSet.Add(2);
sortedSet.Add(8);
sortedSet.Add(2); // Duplicate, won't be added
Console.WriteLine("SortedSet: " + string.Join(", ", sortedSet)); // Output: SortedSet: 2, 5, 8 (always sorted)
}
}
Concepts Behind the Snippet
The snippet showcases the fundamental principles of sets: uniqueness and, in the case of `SortedSet
Real-Life Use Case: Deduplication and Sorting
HashSet
When to Use Them
Use `HashSet
Use `SortedSet
Memory Footprint
The memory footprint of `HashSet
Best Practices
Interview Tip
During interviews, be prepared to discuss the trade-offs between `HashSet
Pros and Cons
HashSet
SortedSet
Alternatives
If you need a sorted collection but don't require uniqueness, consider using a `List
FAQ
-
What is the time complexity of adding an element to a `HashSet
`?
On average, adding an element to a `HashSet` has a time complexity of O(1). However, in the worst-case scenario (e.g., hash collisions), it can degrade to O(n), where n is the number of elements in the set. -
What is the time complexity of adding an element to a `SortedSet
`?
Adding an element to a `SortedSet` has a time complexity of O(log n), where n is the number of elements in the set. This is because `SortedSet ` uses a balanced binary search tree, which requires logarithmic time for insertion. -
How do I ensure that custom objects work correctly with `HashSet
`?
For custom objects to work correctly with `HashSet`, you must override the `GetHashCode()` and `Equals()` methods. `GetHashCode()` should return a hash code that is consistent with the object's equality, and `Equals()` should compare two objects for equality. If two objects are equal according to `Equals()`, they must have the same hash code according to `GetHashCode()`. -
How do I ensure that custom objects work correctly with `SortedSet
`?
For custom objects to work correctly with `SortedSet`, you can implement the `IComparable ` interface on your class. This interface requires you to implement the `CompareTo()` method, which compares the current object to another object of the same type and returns an integer indicating their relative order. Alternatively, you can provide an `IComparer ` instance when constructing the `SortedSet `, which defines a custom comparison logic.