HashSet<T> - Count Property
HashSet<T> (ใน System.Collections.Generic) เป็น Collection ที่เก็บ Unique Values (ไม่ซ้ำ) ใช้ Count Property ดึงจำนวน Elements
ตัวอย่างที่ 1: HashSet Basic Operations
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
HashSet set1 = new HashSet();
set1.Add(25);
set1.Add(50);
set1.Add(75);
set1.Add(100);
set1.Add(125);
set1.Add(150);
Console.WriteLine("Elements in HashSet1:");
foreach (int val in set1) Console.WriteLine(val);
Console.WriteLine($"\nNumber of elements in HashSet1 = {set1.Count}"); // 6
HashSet set2 = new HashSet();
set2.Add(30);
set2.Add(60);
set2.Add(100); // Duplicate with set1
set2.Add(150); // Duplicate with set1
set2.Add(200);
set2.Add(250);
Console.WriteLine("\nElements in HashSet2:");
foreach (int val in set2) Console.WriteLine(val);
Console.WriteLine($"Number of elements in HashSet2 = {set2.Count}"); // 6
// Set Operations
Console.WriteLine($"\nOverlap (Common Elements)? {set1.Overlaps(set2)}"); // True
Console.WriteLine($"Set1 Contains 60? {set1.Contains(60)}"); // False
Console.WriteLine($"Set2 Contains 60? {set2.Contains(60)}"); // True
}
}
ผลลัพธ์:
Elements in HashSet1: 25 50 75 100 125 150 Number of elements in HashSet1 = 6 Elements in HashSet2: 30 60 100 150 200 250 Number of elements in HashSet2 = 6 Overlap (Common Elements)? True Set1 Contains 60? False Set2 Contains 60? True
ตัวอย่างที่ 2: HashSet กับ Duplicate Handling
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
HashSet set1 = new HashSet();
set1.Add("AB");
set1.Add("CD");
set1.Add("EF");
set1.Add("AB"); // Duplicate - Ignored!
set1.Add("IJ");
set1.Add("KL");
set1.Add("EF"); // Duplicate - Ignored!
set1.Add("OP");
Console.WriteLine("Elements in HashSet1:");
foreach (string val in set1) Console.WriteLine(val);
Console.WriteLine($"\nNumber of elements in HashSet1 = {set1.Count}"); // 6 (ไม่ใช่ 8)
HashSet set2 = new HashSet();
set2.Add("EF");
set2.Add("KL");
Console.WriteLine($"\nNumber of elements in HashSet2 = {set2.Count}"); // 2
Console.WriteLine("Elements in HashSet2:");
foreach (string val in set2) Console.WriteLine(val);
// Set Operations
Console.WriteLine($"\nIs Set1 Superset of Set2? {set1.IsSupersetOf(set2)}"); // True
Console.WriteLine($"Is Set1 Subset of Set2? {set1.IsSubsetOf(set2)}"); // False
Console.WriteLine($"Set Equals? {set1.SetEquals(set2)}"); // False
}
}
ผลลัพธ์:
Elements in HashSet1: AB CD EF IJ KL OP Number of elements in HashSet1 = 6 Number of elements in HashSet2 = 2 Elements in HashSet2: EF KL Is Set1 Superset of Set2? True Is Set1 Subset of Set2? False Set Equals? False
HashSet Operations Cheat Sheet
| Operation | Method | คำอธิบาย | Mutates? |
|---|---|---|---|
| Count | .Count | จำนวน Elements | No |
| Add | .Add(item) | เพิ่ม (False ถ้ามีอยู่แล้ว) | Yes |
| Remove | .Remove(item) | ลบ (False ถ้าไม่มี) | Yes |
| Contains | .Contains(item) | ตรวจสอบมีอยู่ไหม O(1) | No |
| Clear | .Clear() | ลบทั้งหมด | Yes |
| Union | .UnionWith(other) | รวม (A ∪ B) | Yes |
| Intersection | .IntersectWith(other) | เฉพาะที่ซ้ำ (A ∩ B) | Yes |
| Difference | .ExceptWith(other) | เฉพาะใน A ไม่อยู่ใน B (A - B) | Yes |
| Symmetric Diff | .SymmetricExceptWith(other) | อยู่ในอันใดอันหนึ่งแต่ไม่ใช่ทั้งคู่ | Yes |
| IsSubsetOf | .IsSubsetOf(other) | A ⊆ B | No |
| IsSupersetOf | .IsSupersetOf(other) | A ⊇ B | No |
| IsProperSubset | .IsProperSubsetOf(other) | A ⊂ B (Strict) | No |
| IsProperSuperset | .IsProperSupersetOf(other) | A ⊃ B (Strict) | No |
| Overlaps | .Overlaps(other) | มี Common Element อย่างน้อย 1 | No |
| SetEquals | .SetEquals(other) | Element เหมือนกันหมด | No |
Performance Characteristics
- O(1) Average สำหรับ Add, Remove, Contains (Hash-based)
- O(n) สำหรับ Union, Intersect, Except (ต้อง Iterate)
- No Ordering - Iteration Order ไม่รับประกัน (แตกต่างจาก SortedSet)
- No Duplicates - Add ค่าซ้ำ Return False, ไม่ Add
HashSet vs List vs Dictionary
| Feature | HashSet<T> | List<T> | Dictionary<TKey,TValue> |
|---|---|---|---|
| Unique Values | ✅ Yes | ❌ No | ✅ Keys Unique |
| Order | ❌ No | ✅ Index | ❌ (.NET Core+ Insertion Order) |
| Contains Speed | O(1) | O(n) | O(1) Key |
| Access by Index | ❌ | ✅ O(1) | ❌ |
| Key-Value | ❌ | ❌ | ✅ |
สรุป
HashSet<T>.Count- ดึงจำนวน Elements (Unique)- Add Duplicate → Ignored (Return False)
- Contains O(1) - เร็วมากกว่า List O(n)
- Set Operations: Union, Intersect, Except, SymmetricExcept
- Subset/Superset/Overlaps/SetEquals สำหรับ Logic Check
- เหมาะสำหรับ: Unique Collection, Fast Lookup, Set Math, Deduplication