> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart Set

A `Set` in Dart is an iterable collection of unique objects. It enforces element uniqueness by evaluating the equality of its items using their `==` operator and `hashCode` properties. Attempting to add a duplicate value to a `Set` is a no-op; the collection remains unchanged and no exception is thrown.

By default, Dart set literals instantiate a `LinkedHashSet`, which maintains insertion order while providing amortized constant-time `O(1)` complexity for add, remove, and lookup operations.

## Syntax and Initialization

Sets are typically created using set literals `{}` or explicitly typed constructors. Because Dart uses `{}` for both sets and maps, creating an empty set requires explicit type annotations to prevent the compiler from inferring a `Map<dynamic, dynamic>`.

```dart theme={"dark"}
// Set literal with type inference
var primaryColors = {'red', 'green', 'blue'}; // Inferred as Set<String>

// Explicitly typed Set literal
Set<int> fibonacci = {1, 2, 3, 5, 8};

// Creating an empty Set
var emptySet = <String>{}; 
Set<double> anotherEmptySet = {};

// Anti-pattern: This creates a Map, not a Set
var notASet = {}; 
```

## Core Operations

The `Set` API provides standard collection mutations and lookups.

```dart theme={"dark"}
var nodes = <String>{'A', 'B'};

// Addition
nodes.add('C');           // Returns true (added)
nodes.add('A');           // Returns false (duplicate, ignored)
nodes.addAll({'D', 'E'}); // Adds multiple elements

// Removal
nodes.remove('B');        // Returns true if element existed and was removed
nodes.clear();            // Empties the set

// Lookup
bool exists = nodes.contains('C');
```

## Mathematical Set Operations

Dart provides built-in methods for standard mathematical set theory operations, returning new `Set` instances without mutating the originals.

```dart theme={"dark"}
var setA = {1, 2, 3, 4};
var setB = {3, 4, 5, 6};

// Union: Combines all unique elements from both sets
// Result: {1, 2, 3, 4, 5, 6}
var union = setA.union(setB);

// Intersection: Yields elements present in both sets
// Result: {3, 4}
var intersection = setA.intersection(setB);

// Difference: Yields elements in setA that are not in setB
// Result: {1, 2}
var difference = setA.difference(setB);
```

## Alternative Implementations

While `LinkedHashSet` is the default, the `dart:collection` library provides alternative implementations with different performance and ordering guarantees:

```dart theme={"dark"}
import 'dart:collection';

// HashSet: Unordered. Iteration order is not guaranteed. 
// Marginally faster than LinkedHashSet as it doesn't maintain pointers for insertion order.
var hashSet = HashSet<int>();

// SplayTreeSet: A self-balancing binary search tree. 
// Elements are iterated in sorted order according to their `compareTo` method 
// or a provided Comparator. Operations are O(log n).
var treeSet = SplayTreeSet<int>();
```

## Spread Operator and Control Flow

Sets fully support Dart's spread operator (`...`), null-aware spread operator (`...?`), and collection `if`/`for` constructs for declarative composition.

```dart theme={"dark"}
var baseConfig = {'host', 'port'};
bool includeAuth = true;

var finalConfig = <String>{
  ...baseConfig,
  if (includeAuth) 'credentials',
  for (var i = 1; i <= 3; i++) 'node_$i'
};
// Result: {'host', 'port', 'credentials', 'node_1', 'node_2', 'node_3'}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
