Dart – Remove Element from Set

To remove a specific element from a Set in Dart, call remove() method on this Set, and pass the element as argument to the method. If the element is removed, then the method returns true, else it returns false.

remove() method can remove the element from the Set, only if the element is present.

Syntax

The syntax to remove an element e from the Set mySet is

mySet.remove(e);

remove() method modifies the original Set.

ADVERTISEMENT

Examples

In the following program, we take a Set of strings mySet, and remove the element 'banana' from the Set.

main.dart

void main() {
  Set mySet = {'apple', 'banana', 'cherry'};
  print('Original Set  : $mySet');
  mySet.remove('banana');
  print('Resulting Set : $mySet');
}

Output

Original Set  : {apple, banana, cherry}
Resulting Set : {apple, cherry}

Conclusion

In this Dart Tutorial, we learned how to remove a given element from a Set in Dart language, with examples.