Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

set
dart
remove
communitycreator

What is the set.remove() method in Dart?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The set.remove() function in Dart is used to remove a specific element from a set.

The image below shows a visual representation of the set.remove() function:

Visual representation of the "set.remove()" function

Note: In Dart, a set is a particular instance of a list in which all of the inputs are unique.

Note: dart:core is required in order to use this function.


Note: If there are duplicates, then the set will only keep the first instance. For example, {'Tom','Alsvin','Eddie','Tom'} will result in {'Tom','Alsvin','Eddie'}, after the set.remove() function is called.

Syntax

bool set_name.remove(element)
// where the set_name is the name of the set.

Parameter

This function takes the element that is to be removed as a parameter.

Return value

This function removes the element that is sent as a parameter from the set. It returns True if the element is present in the set. Otherwise, it returns False.

Code

The code given below shows us how to use the set.remove() function in Dart:

import 'dart:convert';
import 'dart:core';
void main() {
Set<String> set_1 = {'Tom','Alsvin','Eddie'};
//set containing value before remove
print("Initial set_1 elements: ${set_1}");
//removing element from set
print("Removing 'Eddie' from the set: ${set_1.remove('Eddie')}");
//set containing value after remove
print("set_1 elements after removing 'Eddie': ${set_1}");
//trying to remove element not present in set
print("Removing 'Jack' from the set: ${set_1.remove('Jack')}");
}
Removing elements from the "set" using the "set.remove()" function

RELATED TAGS

set
dart
remove
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring