Open In App

Dart – Basics of Set

Last Updated : 14 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Set is a special type of collection in Dart programming. In which any object can occur only once. If we perform add operation in the set, if the object is already in the set then it will not add otherwise it will be added in the set.  dart: core library provides the Set class for implementation.

Syntax:  Identifier = new Set()

or

Identifier = new Set.from(Iterable)

Here, Iterable is the list of value which will be added in the set .

Example 1:
 

Dart




void main() { 
    
   // create a new set number
   Set number = new  Set(); 
    
   // add in the set 
   number.add(1); 
   number.add(2); 
   number.add(5);
   number.add(1);  
     
      
   // all elements are retrieved 
   // in the order in which 
   // they are inserted 
   for(var no in number) { 
      print(no); 
   
}


Output:

1
2
5

Example 2: 
 

Dart




void main() {
    
   // Create a Set Number Which
   // contains a list of value
   Set number = new Set.from([1,11,14,11]); 
      
   // all elements are retrieved
   // in the order in which 
   // they are inserted 
   for(var n in number) { 
      print(n); 
   
}


Output:

1
11
14

Example 3: 
 

Dart




void main() { 
    
   // Create a Set Number Which
   // contains a list of value
   Set number = new Set.from(['geeks','for','geeks']); 
      
   // all elements are retrieved 
   // in the order in which 
   // they are inserted 
   for(var n in number) { 
      print(n); 
   
}


Output:

geeks
for


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads