Để xóa một phần tử cụ thể khỏi SortedSet<T>, bạn sử dụng phương thức Remove(T item). Phương thức này trả về true nếu phần tử được tìm thấy và xóa thành công, false nếu phần tử không tồn tại.
Ví dụ 1: Xóa nhiều phần tử khỏi SortedSet<string>
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("AB");
set1.Add("BC");
set1.Add("CD");
set1.Add("EF");
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (string res in set1)
{
Console.WriteLine(res);
}
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("\nCác phần tử trong SortedSet2...");
foreach (string res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("Số lượng phần tử trong SortedSet = " + set2.Count);
// Xóa các phần tử cụ thể
bool removedHI = set2.Remove("HI");
bool removedJK = set2.Remove("JK");
Console.WriteLine($"Xóa 'HI' thành công? {removedHI}");
Console.WriteLine($"Xóa 'JK' thành công? {removedJK}");
Console.WriteLine("Số lượng phần tử trong SortedSet (đã cập nhật) = " + set2.Count);
Console.WriteLine("\nCác phần tử còn lại:");
foreach (string res in set2)
{
Console.WriteLine(res);
}
}
}
Kết quả
Các phần tử trong SortedSet1... AB BC CD EF Các phần tử trong SortedSet2... AB BC CD DE EF HI JK Số lượng phần tử trong SortedSet = 7 Xóa 'HI' thành công? True Xóa 'JK' thành công? True Số lượng phần tử trong SortedSet (đã cập nhật) = 5 Các phần tử còn lại: AB BC CD DE EF
Ví dụ 2: Xóa phần tử khỏi SortedSet<int>
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
Console.WriteLine("Các phần tử trong SortedSet...");
foreach (int res in set1)
{
Console.WriteLine(res);
}
Console.WriteLine("Số lượng phần tử trong SortedSet = " + set1.Count);
bool removed = set1.Remove(100);
Console.WriteLine($"Xóa 100 thành công? {removed}");
Console.WriteLine("Số lượng phần tử trong SortedSet (đã cập nhật) = " + set1.Count);
Console.WriteLine("\nCác phần tử còn lại:");
foreach (int res in set1)
{
Console.WriteLine(res);
}
}
}
Kết quả
Các phần tử trong SortedSet... 100 200 300 400 Số lượng phần tử trong SortedSet = 4 Xóa 100 thành công? True Số lượng phần tử trong SortedSet (đã cập nhật) = 3 Các phần tử còn lại: 200 300 400
Các phương thức xóa khác của SortedSet
| Phương thức | Mô tả | Trả về |
|---|---|---|
Remove(T item) |
Xóa phần tử cụ thể | bool - true nếu xóa thành công |
RemoveWhere(Predicate<T>) |
Xóa tất cả phần tử thỏa điều kiện | int - số phần tử đã xóa |
Clear() |
Xóa tất cả phần tử | void |
ExceptWith(IEnumerable<T>) |
Xóa phần tử có trong collection khác | void |
Ví dụ RemoveWhere
SortedSet<int> set = new SortedSet<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Xóa các số chẵn
int countRemoved = set.RemoveWhere(x => x % 2 == 0);
Console.WriteLine($"Đã xóa {countRemoved} phần tử");
// Kết quả: 1, 3, 5, 7, 9
Lưu ý quan trọng
Remove()trả vềfalsenếu phần tử không tồn tại - không ném exception- SortedSet tự động duy trì thứ tự sắp xếp sau khi xóa
- Phức tạp thời gian: O(log n) cho Remove đơn lẻ
- Không thể xóa bằng index (SortedSet không hỗ trợ indexing)
- Để xóa Min/Max:
set.Remove(set.Min)hoặcset.Remove(set.Max)
So sánh với HashSet
// HashSet - không sắp xếp, O(1) trung bình HashSet<int> hashSet = new HashSet<int>(); hashSet.Remove(100); // O(1) // SortedSet - tự động sắp xếp, O(log n) SortedSet<int> sortedSet = new SortedSet<int>(); sortedSet.Remove(100); // O(log n)