Dart Set remove()
Syntax & Examples
Set.remove() method
The `remove` method in Dart removes a specified value from the set. It returns true if the value was present in the set and successfully removed, otherwise returns false. If the value was not in the set, the method has no effect.
Syntax of Set.remove()
The syntax of Set.remove() method is:
bool remove(Object value)
This remove() method of Set removes value from the set. Returns true if value was in the set. Returns false otherwise. The method has no effect if value value was not in the set.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to be removed from the set. |
Return Type
Set.remove() returns value of type bool
.
✐ Examples
1 Remove integer from set
In this example,
- We create a Set
set
containing integers. - We use the
remove()
method to remove the integer value 3 from the set. - We print whether the removal was successful to standard output.
Dart Program
void main() {
Set<int> set = {1, 2, 3, 4};
bool removed = set.remove(3);
print('Is 3 removed from set? $removed');
}
Output
Is 3 removed from set? true
2 Remove string from set
In this example,
- We create a Set
set
containing strings. - We use the
remove()
method to remove the string value 'kiwi' from the set. - We print whether the removal was successful to standard output.
Dart Program
void main() {
Set<String> set = {'apple', 'banana', 'orange'};
bool removed = set.remove('kiwi');
print('Is kiwi removed from set? $removed');
}
Output
Is kiwi removed from set? false
Summary
In this Dart tutorial, we learned about remove() method of Set: the syntax and few working examples with output and detailed explanation for each example.