Skip to Content

Set trong Dart

🎯 Mục tiêu: Làm việc với Set - collection không trùng lặp.


💡 Tạo Set

var fruits = {'Apple', 'Banana', 'Orange'}; Set<int> numbers = {1, 2, 3}; var empty = <String>{}; // Từ List var unique = [1, 2, 2, 3, 3, 3].toSet(); // {1, 2, 3}

📝 Operations

var set = {1, 2, 3}; set.add(4); // {1, 2, 3, 4} set.add(2); // {1, 2, 3, 4} - đã có set.remove(1); // {2, 3, 4} set.contains(2); // true

🔧 Set Operations

var a = {1, 2, 3}; var b = {2, 3, 4}; a.union(b); // {1, 2, 3, 4} a.intersection(b); // {2, 3} a.difference(b); // {1}

✅ Checklist

  • Set không có phần tử trùng
  • Dùng cho deduplicate
  • Union, intersection, difference

Last updated on