Dart – Basics of Set

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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); 
   
}

chevron_right


Output:

1
2
5

Example 2: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

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); 
   
}

chevron_right


Output:

1
11
14

Example 3: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

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); 
   
}

chevron_right


Output:

geeks
for



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.