- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
? HashSet in C# — A Must-Know Tool for Uniqueness and Fast Lookups
HashSet<T> is one of the most versatile and commonly used data structures in coding interviews. It allows constant-time lookup and guarantees unique elements only.
When to Use HashSet
Declaring and Using HashSet
var seen = new HashSet<int>();
seen.Add(5); // true
seen.Add(5); // false (duplicate)
bool exists = seen.Contains(5); // true
seen.Remove(5); // removes 5
? Looping Through HashSet
foreach (int num in seen)
{
Console.WriteLine(num);
}
? Interview Example 1: Detect Duplicates
public bool ContainsDuplicate(int[] nums)
{
var set = new HashSet<int>();
foreach (int num in nums)
{
if (!set.Add(num)) return true;
}
return false;
}
? Interview Example 2: Longest Substring Without Repeating Characters
public int LengthOfLongestSubstring(string s)
{
var set = new HashSet<char>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.Length; right++)
{
while (set.Contains(s
HashSet<T> is one of the most versatile and commonly used data structures in coding interviews. It allows constant-time lookup and guarantees unique elements only.
| Use Case | Why Use HashSet |
|---|---|
| Check for duplicates | Fast Contains() check |
| Track seen items | O(1) add/lookup |
| Unique collections | Prevents duplicates automatically |
| Set operations (union/intersect) | Built-in set logic |
var seen = new HashSet<int>();
seen.Add(5); // true
seen.Add(5); // false (duplicate)
bool exists = seen.Contains(5); // true
seen.Remove(5); // removes 5
? Looping Through HashSet
foreach (int num in seen)
{
Console.WriteLine(num);
}
? Interview Example 1: Detect Duplicates
public bool ContainsDuplicate(int[] nums)
{
var set = new HashSet<int>();
foreach (int num in nums)
{
if (!set.Add(num)) return true;
}
return false;
}
? Interview Example 2: Longest Substring Without Repeating Characters
public int LengthOfLongestSubstring(string s)
{
var set = new HashSet<char>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.Length; right++)
{
while (set.Contains(s
))
{
set.Remove(s
{
set.Remove(s
);
left++;
}
set.Add(s
left++;
}
set.Add(s
);
maxLen = Math.Max(maxLen, right - left + 1);
}
return maxLen;
}
Set Operations
var a = new HashSet<int>() { 1, 2, 3 };
var b = new HashSet<int>() { 2, 3, 4 };
a.IntersectWith(b); // a = {2, 3}
a.UnionWith(b); // a = {1, 2, 3, 4}
a.ExceptWith(b); // a = {1}
? Summary
Next up: Dictionary — perfect for counting, mapping, and solving problems like Two Sum and Group Anagrams.
maxLen = Math.Max(maxLen, right - left + 1);
}
return maxLen;
}
var a = new HashSet<int>() { 1, 2, 3 };
var b = new HashSet<int>() { 2, 3, 4 };
a.IntersectWith(b); // a = {2, 3}
a.UnionWith(b); // a = {1, 2, 3, 4}
a.ExceptWith(b); // a = {1}
? Summary
| Feature | Syntax Example |
|---|---|
| Declare | new HashSet<int>() |
| Add | set.Add(value) |
| Check Exists | set.Contains(value) |
| Remove | set.Remove(value) |
| Set Ops | UnionWith(), IntersectWith() |
Next up: Dictionary — perfect for counting, mapping, and solving problems like Two Sum and Group Anagrams.