Open In App

Dart - Sets

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sets in Dart is a special case in List, where all the inputs are unique i.e. it doesn't contain any repeated input. It can also be interpreted as an unordered array with unique inputs. The set comes into play when we want to store unique values in a single variable without considering the order of the inputs.

The sets are declared by the use of a set keyword. There are two ways to do so: 

// Method 1
var variable_name = <variable_type>{};

// Method 2
Set <variable_type> variable_name = {};

Example 1: Declaring set in two different ways.

Dart
// Dart program to show the Sets concept
void main()
{
      // Declaring set in First Way
      var gfg1 = <String>{'GeeksForGeeks'}; 
      
      // Printing First Set
      print("Output of first set: $gfg1");
      
      // Declaring set in Second Way
      Set<String> gfg2 = {'GeeksForGeeks'};  
      
      // Printing Second Set
      print("Output of second set: $gfg2");
}


Output: 

Output of first set: {GeeksForGeeks}
Output of second set: {GeeksForGeeks}


Example 2: Declaring repeated value in a set and a list and then comparing it. 

Dart
// Dart Program to declare repeated value 
// in a set and a list and then 
// comparing it

void main()
{
      // Declaring list with repeated value
      var gfg = ['Geeks','For','Geeks'];
      
      // Printing List
      print("Output of the list is: $gfg");
      
      // Declaring set with repeated value
      var gfg1 = <String>{'Geeks','For','Geeks'};  
      
      // Printing Set
      print("Output of the set is: $gfg1");
}


Output: 

Output of the list is: [Geeks, For, Geeks]
Output of the set is: {Geeks, For}

Note: You can see that repeated value was simply ignored in the case of the set.


Adding Element In Set

To add an element in the set we make use of ".add()" function or ".addAll()" function. But you must note that if you try to add a duplicate value using these functions then too they will get ignored in a set.

 // To add single value
variable_name.add(value);

// To add multiple value
variable_name.addAll(value1, value2, value3, ...valueN)the


Some other functions involving Sets are as follows: 

S. No.SyntaxDescription
1.variable_name.elementAt(index);It returns the element at the corresponding index. The type of output depends on the type of set defined.
2.variable_name.length;It returns the length of the set. The output is of integer type.
3.variable_name.contains(element_name);It returns boolean value true if the element_name is present in the set else return false.
4.variable_name.remove(element_name);It deletes the element whose name is present inside it.
5.variable_name.forEach(...);It runs the command defined inside forEach() function for all the elements inside the set.
6.variable_name.clear();It deletes all the element inside the set.

Example: 

Dart
void main()
{
      // Declaring set with value
      var gfg = <String>{'Hello Geek'};
      
      // Printing Set
      print("Value in the set is: $gfg");
      
      // Adding an element in the set
      gfg.add("GeeksForGeeks");
      
      // Printing Set
      print("Values in the set is: $gfg");
      
      // Adding multiple values to the set
      var geeks_name = {"Geek1","Geek2","Geek3"};
      gfg.addAll(geeks_name);
      
      // Printing Set
      print("Values in the set is: $gfg");
      
      // Getting element at Index 0
      var geek = gfg.elementAt(0);
      
      // Printing the element
      print("Element at index 0 is: $geek");
      
      // Counting the length of the set
      int l = gfg.length;
      
      // Printing length
      print("Length of the set is: $l");
      
      // Finding the element in the set
      bool check = gfg.contains("GeeksForGeeks");
      
      // Printing boolean value
      print("The value of check is: $check");
      
      // Removing an element from the set
      gfg.remove("Hello Geek");
      
      // Printing Set
      print("Values in the set is: $gfg");
      
      // Using forEach in set
      print(" ");
      print("Using forEach in set:");
      gfg.forEach((element) {
        if(element == "Geek1")
        {
          print("Found");
        }
        else
        {
          print("Not Found");
        }
      });
      
      // Deleting elements from set
      gfg.clear();
      
      // Printing set
      print("Values in the set is: $gfg");
}


Output: 

Value in the set is: {Hello Geek}
Values in the set is: {Hello Geek, GeeksForGeeks}
Values in the set is: {Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3}
Element at index 0 is: Hello Geek
Length of the set is: 5
The value of check is: true
Values in the set is: {GeeksForGeeks, Geek1, Geek2, Geek3}

Using forEach in set:
Not Found
Found
Not Found
Not Found
Values in the set is: {}


Converting Set to List in Dart

Dart also provides us with the method to convert the set into the list. To do, so we make use of toList() method in Dart.

List<type> list_variable_name = set_variable_name.toList();

Note: It is useful in the way as the list we will get will contain unique values and no repeated values.

Example: 

Dart
// Converting Set to List in Dart
void main()
{
      // Declaring set with value
      var gfg = <String>{'Hello Geek',"GeeksForGeeks","Geek1","Geek2","Geek3"};  
      
      // Printing values in set
      print("Values in set are:");
      print(gfg);
      
      print("");
      
      // Converting Set to List
      List<String> gfg_list = gfg.toList();
      
      // Printing values of list
      print("Values in the list are:");
      print(gfg_list);
}


Output: 

Values in set are:
{Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3}

Values in the list are:
[Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3]


Converting Set to Map in Dart 

Dart also provides us with the method to convert the set into the map. 

Example: 

Dart
// Converting Set to Map in Dart
void main()
{
      // Declaring set 1 with value
      var gfg = <String>{"GeeksForGeeks","Geek1","Geek2","Geek3"};
      
      var geeksforgeeks = gfg.map((value) {
        return 'mapped $value';
      });
      print("Values in the map:");
      print(geeksforgeeks);
    
}


Output:  

Values in the map:
(mapped GeeksForGeeks, mapped Geek1, mapped Geek2, mapped Geek3)


Set Operations in Dart

There are three operations on set in Dart: 

Example:  

Dart
// Set Operations in Dart

void main()
{
      // Declaring set 1 with value
      var gfg1 = <String>{"GeeksForGeeks","Geek1","Geek2","Geek3"};
      
      // Printing values in set
      print("Values in set 1 are:");
      print(gfg1);
      
      print("");
      
      // Declaring set 2 with value
      var gfg2 = <String>{"GeeksForGeeks","Geek3","Geek4","Geek5"};
      
      // Printing values in set
      print("Values in set 2 are:");
      print(gfg2);
      
      print("");
      
      
      // Finding Union
      print("Union of two sets is ${gfg1.union(gfg2)} \n");
      
      // Finding Intersection
      print("Intersection of two sets is ${gfg1.intersection(gfg2)} \n");
      
      // Finding Difference
      print("Difference of two sets is ${gfg2.difference(gfg1)} \n");
        
}


Output:  

Values in set 1 are:
{GeeksForGeeks, Geek1, Geek2, Geek3}

Values in set 2 are:
{GeeksForGeeks, Geek3, Geek4, Geek5}

Union of two sets is {GeeksForGeeks, Geek1, Geek2, Geek3, Geek4, Geek5}

Intersection of two sets is {GeeksForGeeks, Geek3}

Difference of two sets is {Geek4, Geek5}


In the above code, we can also compare more than two sets as: 

Example: 

Dart
// Comparing more than 2 sets in Dart

void main()
{
      // Declaring set 1 with value
      var gfg1 = <String>{"GeeksForGeeks","Geek1","Geek2","Geek3"};
      
      // Declaring set 2 with value
      var gfg2 = <String>{"GeeksForGeeks","Geek3","Geek4","Geek5"};
      
      // Declaring set 3 with value
      var gfg3 = <String>{"GeeksForGeeks","Geek5","Geek6","Geek7"};
      
      // Finding Union
      print("Union of two sets is ${gfg1.union(gfg2).union(gfg3)}\n");
      
      // Finding Intersection
      print("Intersection of two sets is ${gfg1.intersection(gfg2).intersection(gfg3)}\n");
      
      // Finding Difference
      print("Difference of two sets is ${gfg2.difference(gfg1).difference(gfg3)}\n");
}


Output: 

Union of two sets is {GeeksForGeeks, Geek1, Geek2, Geek3, Geek4, Geek5, Geek6, Geek7} 

Intersection of two sets is {GeeksForGeeks}

Difference of two sets is {Geek4}


Conclusion

Sets in Dart provide an efficient way to store unique values without maintaining a specific order. They are particularly useful when working with collections that need to ensure uniqueness, such as when removing duplicate elements from a list. Dart offers various methods for adding and removing elements, as well as performing set operations like union, intersection, and difference. Understanding how to work with sets can optimize data storage and retrieval in Dart applications.


Next Article
Article Tags :

Similar Reads