A set in the Dart programming language is defined as a collection of multiple elements of the same data type. However, each of these elements needs to be unique. In this guide, we will study the usage of the Dart sets in Ubuntu 20.04 by going through some simple yet helpful examples.

Using the Sets in the Dart Programming Language in Ubuntu 20.04:

We have implemented the following seven examples with you to teach you the uses of the sets in the Dart programming language. Each of these examples will introduce you to a new concept associated with the usage of the sets in the Dart programming language.

Example # 1: Declaring a Set by using the Variable Notation in the Dart Programming Language:

There are two different ways of declaring a set in the Dart programming language. We will share with you the first method of doing so. For that, you will have to go through the Dart script shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image1-2.png" data-lazy- height="240" src="data:image/svg xml,” width=”704″>

We have first declared a set named “months” within our “main()” function by making use of the variable notation by using the “var” keyword. This is a “String” type set and contains six different months as its elements. Then, we have just used the “print” statement for printing these elements on the terminal.

To run this Dart script, we have used the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image3-2.png" data-lazy- height="26" src="data:image/svg xml,” width=”411″>

All the elements of our Dart set are shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image2-2.png" data-lazy- height="77" src="data:image/svg xml,” width=”432″>

Example # 2: Declaring a Set by using the Set Notation in the Dart Programming Language:

In this example, we will be sharing with you the second notation for declaring a set in the Dart programming language. For that, you will have to take a look at the following Dart script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image5-3.png" data-lazy- height="246" src="data:image/svg xml,” width=”707″>

In this Dart script, we have declared the same set of “months” that we have used in the first example. However, this time, we have declared it by using the “set” notation by making use of the “Set” keyword. After declaring and initializing this set, we have just printed its elements on the console.

Upon executing this Dart script, all the elements of our “months” set appeared on the terminal as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image4-1.png" data-lazy- height="81" src="data:image/svg xml,” width=”437″>

Example # 3: A Set Containing Duplicate Elements in the Dart Programming Language:

Now, when you have learned the two different declarations of the sets in the Dart programming language, we will try to move a step further. We will teach you what happens if you have duplicate elements within a Dart set. For that, you will have to consider the following Dart script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image7-1.png" data-lazy- height="257" src="data:image/svg xml,” width=”703″>

In this example, we have declared the “months” set in the Dart programming language. However, this time, it contains two duplicate values (i.e. March and September.) To check what happens to this set because of the existence of these duplicate values, we have used a “print” statement for printing the elements of this set on the console.

The output of this Dart script is shown in the image below which tells us that all the duplicate elements present within a Dart set are printed only once.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image6-1.png" data-lazy- height="79" src="data:image/svg xml,” width=”413″>

Example # 4: Adding an Element to a Set in the Dart Programming Language:

In this example, we will teach you how you can add an element to an existing set in the Dart programming language. For that, you need to look at the following Dart script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image9-1.png" data-lazy- height="291" src="data:image/svg xml,” width=”703″>

In this script, we have used the same “months” set that we have used in the examples above. However, this time, we have added a new element to this set by using the “months.add(“May”)” statement. After this modification, we have printed the elements of this set on the terminal again.

The output of this Dart script is shown in the image below. This Dart script has correctly printed the elements of the set after adding a new element to it. Moreover, all the newly added elements to a set are always inserted at the end.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image8-1.png" data-lazy- height="115" src="data:image/svg xml,” width=”430″>

Example # 5: Removing an Element from a Set in the Dart Programming Language:

You can also remove a particular element from a Dart set at any time you want.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image12.png" data-lazy- height="292" src="data:image/svg xml,” width=”608″>

In this example, we have just used the “months.remove(“January”)” statement for removing the month “January” from our set. For verifying this removal, we have printed the updated elements of the set on the terminal.

The output of this Dart program is shown in the image below that confirms that our specified element has been successfully removed from our set.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image10-1.png" data-lazy- height="106" src="data:image/svg xml,” width=”432″>

Example # 6: Checking the Length of a Set in the Dart Programming Language:

You can also check the length of a Dart set by making use of the Dart script that follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image11.png" data-lazy- height="266" src="data:image/svg xml,” width=”702″>

In this Dart program, we have just used the “${months.length}” notation for printing the length of the Dart set on the terminal.

The length of our Dart set in this particular example is “4” as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image13.png" data-lazy- height="98" src="data:image/svg xml,” width=”457″>

Example # 7: Accessing a Particular Index of a Set in the Dart Programming Language:

If you wish to access any particular element or index of a Dart set, then you will have to proceed as explained in the following Dart script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image14.png" data-lazy- height="255" src="data:image/svg xml,” width=”700″>

In this example, we wanted to access the element at the 0th index of our set for which we have simply used the “${months.elementAt(0)}” notation. You can replace “0” with any other index of your choice depending upon the index of the set that you wish to access.

The element present at the 0th index of our set is shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/image15.png" data-lazy- height="90" src="data:image/svg xml,” width=”439″>

Conclusion:

This article was written to demonstrate the usage of the Dart sets in Ubuntu 20.04. We tried to explain this concept to you with the help of seven simple examples. After going through these examples, you will not only get familiar with the declaration of the Dart sets but you will also be able to manipulate them by making use of the different associated functions.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/IMG-20211027-WA0029-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.