Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
set
clear
communitycreator

What is the set.clear() 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.clear() function in Dart is used to remove all the elements in the set.

The illustration below shows the visual representation of the set.clear() function:

Figure 1: Visual representation of set.clear() function

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

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


If there are duplicates in a set then the set will only keep the first instance. For example, {'Tom','Alsvin','Eddie','Tom'} results in {'Tom','Alsvin','Eddie'}.

Syntax

void set_name.clear()

set_name is the name of the set.

Parameters

The set.clear() function does not require any parameters.

Return value

The function returns nothing while the calling set is cleared in-place.

Code

The following code shows how to use set.clear() function in Dart:

import 'dart:convert';
import 'dart:core';
void main() {
Set<String> set_1 = {'Tom','Alsvin','Eddie'};
//set containing unique value
print("set_1 elements before clear: ${set_1}");
//set clear
set_1.clear();
print("set_1 elements after clear: ${set_1}");
}

RELATED TAGS

dart
set
clear
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