Dart – Basics of Set
Last Updated :
14 Jul, 2020
Set is a special type of collection in Dart programming. In which any object can occur only once. If we perform add operation in the set, if the object is already in the set then it will not add otherwise it will be added in the set. dart: core library provides the Set class for implementation.
Syntax: Identifier = new Set()
or
Identifier = new Set.from(Iterable)
Here, Iterable is the list of value which will be added in the set .
Example 1:
Dart
void main() {
Set number = new Set();
number.add(1);
number.add(2);
number.add(5);
number.add(1);
for (var no in number) {
print(no);
}
}
|
Output:
1
2
5
Example 2:
Dart
void main() {
Set number = new Set.from([1,11,14,11]);
for (var n in number) {
print(n);
}
}
|
Output:
1
11
14
Example 3:
Dart
void main() {
Set number = new Set.from([ 'geeks' , 'for' , 'geeks' ]);
for (var n in number) {
print(n);
}
}
|
Output:
geeks
for
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...