To get the length of a set in Dart, you can use the length
property of the Set
class.
In this example,
mySet
and initialize it with a set of string elements using the Set
class.length
property of mySet
to get the number of elements in the set.print
.void main() {
// Declare and initialize a set
Set<String> mySet = {'apple', 'banana', 'orange'};
// Get the length of the set
int length = mySet.length;
// Print the length of the set
print('Length of the set: $length');
}
Length of the set: 3
In this example,
emptySet
using the Set
class and initialize it with no elements.length
property of emptySet
to get the number of elements in the set.print
.void main() {
// Declare and initialize an empty set
Set<String> emptySet = {};
// Get the length of the empty set
int length = emptySet.length;
// Print the length of the empty set
print('Length of the empty set: $length');
}
Length of the empty set: 0
In this tutorial, we learned How to Get Length of a Set in Dart language with well detailed examples.