-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.dart
More file actions
53 lines (44 loc) · 904 Bytes
/
Set.dart
File metadata and controls
53 lines (44 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void main() {
//Set
var numbers = {1, 2, 3};
//Using curly braces
Set<int> ids = {101, 102, 103};
// Using the Set constructor
Set<String> fruits = Set();
fruits.add("Apple");
fruits.add('Banana');
// Add values
Set<String> colors = {'Red', 'Green'};
colors.add('Blue');
colors.add('Red');
print(colors);
//Remove values
colors.remove('Green');
print(colors);
//Check existence
print(colors.contains('Red'));
print(colors.contains('Pink'));
//Loop through a Set
for (var color in colors) {
print(color);
}
colors.forEach((color) => print(color));
//Set Methods
Set<int> set1 = {1, 2, 3};
Set<int> set2 = {3, 4, 5};
print(set1.union(set2));
print(set1.intersection(set2));
print(set1.difference(set2));
}
//output
// {Red, Green, Blue}
// {Red, Blue}
// true
// false
// Red
// Blue
// Red
// Blue
// {1, 2, 3, 4, 5}
// {3}
// {1, 2}