# Dart Set

last modified January 9, 2023

In this article we show how to work with a Set collection in Dart language.

A set is a collection of data with no duplicate elements. A set supports operations like union, intersection, or difference, known from mathematics.

There are three types of set collections in Dart. A `HashSet` is unordered, which means that its iteration order is unspecified, `LinkedHashSet` iterates in the insertion order of its elements, and a sorted set `SplayTreeSet` iterates the elements in sorted order.

A set can be created with a pair of `{}` brackets. This syntax is called the collection literal notation.

## Dart set example

A new item is inserted to a set with `add`. The size of a set is determined with `length` property.

main.dart
```void main() {
var brands = <String>{};

brands.add("Wilson");
brands.add("Nike");
brands.add("Volvo");
brands.add("IBM");
brands.add("IBM");

int n = brands.length;

print("The set contains \$n elements");
print(brands);
}
```

In the program, we have a set of brands.

```var brands = <String>{};
```

A set is created with a pair of `{}`. In the `<>` brackets we provide the set's data type.

```brands.add("Wilson");
brands.add("Nike");
brands.add("Volvo");
brands.add("IBM");
brands.add("IBM");
```

Elements are inserted with `add`. Even though we add `IBM` twice, the final set contains it only once.

```int n = brands.length;
```

We get the length of the set.

```\$ dart main.dart
The set contains 4 elements
{Wilson, Nike, Volvo, IBM}
```

## Dart set remove elements

In the next example, we remove elements from a set.

main.dart
```void main() {
var brands = <String>{};

brands.add("Wilson");
brands.add("Nike");
brands.add("Volkswagen");
brands.add("Volvo");
brands.add("IBM");
brands.add("Kia");
brands.add("Lenovo");

print(brands);

brands.remove('Volvo');

print(brands);

brands.removeWhere((e) => e.length == 3);

print(brands);

brands.clear();

print(brands);

print("The set contains \${brands.length} elements");
}
```

The elements are removed with `remove`, `removeWhere`, and `clear`.

```brands.remove('Volvo');
```

The specified element is removed with `remove`.

```brands.removeWhere((e) => e.length == 3);
```

With `removeWhere`, we delete all elements that satisfy the given predicate function.

```brands.clear();
```

The `clear` method removes all elements.

```\$ dart main.dart
{Wilson, Nike, Volkswagen, Volvo, IBM, Kia, Lenovo}
{Wilson, Nike, Volkswagen, IBM, Kia, Lenovo}
{Wilson, Nike, Volkswagen, Lenovo}
{}
The set contains 0 elements
```

## Dart iterate set

In the next example, we iterate over the elements of a set.

main.dart
```import 'dart:collection';

void main() {
var nums = HashSet<int>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(4);
nums.add(5);

nums.forEach((e) {
print(e);
});

for (var e in nums) {
print(e);
}
}
```

We can loop over the elements with `forEach` function or the `for` loop.

```var nums = HashSet<int>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(4);
nums.add(5);
```

We create a instance of the `HashSet` and insert some integers with `add`.

```nums.forEach((e) {
print(e);
});
```

We iterate over the elements with the built-in `forEach` method.

```for (var e in nums) {
print(e);
}
```

Here we use the for loop.

## Dart set insertion order

The `HashSet` does not keep the insertion order of the elements. The `LinkedHashSet` does keep.

main.dart
```import 'dart:collection';

void main() {
var nums1 = HashSet<int>();
nums1.add(4);
nums1.add(3);
nums1.add(2);
nums1.add(1);

print(nums1);
print(nums1.runtimeType);

nums1.forEach((e) {
print(e);
});

print("-----------------------");

var nums2 = LinkedHashSet<int>();
nums2.add(4);
nums2.add(3);
nums2.add(2);
nums2.add(1);

print(nums2);
print(nums2.runtimeType);

nums2.forEach((e) {
print(e);
});
}
```

We compare the order of the elements created with `HashSet` and `LinkedHashSet`.

```print(nums1.runtimeType);
```

The type of the object can be determined with `runtimeType`.

```\$ dart main.dart
{1, 2, 3, 4}
_HashSet<int>
1
2
3
4
--------------
{4, 3, 2, 1}
_CompactLinkedHashSet<int>
4
3
2
1
```

## Dart set functions

There are some functions that are specific to sets, such as union.

main.dart
```void main() {
var vals1 = <int>{1, 2, 3, 4, 5, 6, 7};
var vals2 = <int>{6, 7, 8, 9, 10};

var res = vals1.union(vals2);
print(res);

var res2 = vals1.intersection(vals2);
print(res2);

var res3 = vals1.difference(vals2);
print(res3);
}
```

In the program, we use `union`, `intersetction`, and `difference` functions.

```var res = vals1.union(vals2);
```

The `union` function returns elements from both sets.

```var res2 = vals1.intersection(vals2);
```

The `intersection` returns only elements that are present in both sets.

```var res3 = vals1.difference(vals2);
```

The `difference` function carries out the difference operation, which returns elements that are in the `vals1` but not in `vals2`.

```\$ dart main.dart
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{6, 7}
{1, 2, 3, 4, 5}
```

In this article, we have covered Dart sets.

List all Dart tutorials.