Dart – 19 – Sets

Sets in Dart Programming

Sets are an essential data structure in Dart that provide a way to store unique values. Unlike lists, sets do not allow duplicate elements, making them ideal for situations where you need to work with distinct items. In this discussion, we’ll explore the significance of sets in Dart, understand how they work, and see practical examples of their use.

Understanding Sets

A set in Dart is an unordered collection of distinct values. It does not allow duplicates, ensuring that every value is unique. Sets are often used to store unique items or to check for the presence of specific elements.

Creating Sets

In Dart, you can create a set using a set literal or by instantiating a Set object. Set literals are enclosed in curly braces {}, while Set objects are created using constructors.

Here’s an example of creating sets in Dart:


// Using set literals
var colors = {'red', 'green', 'blue'};

// Using the Set constructor
var uniqueNumbers = Set();
uniqueNumbers.add(1);
uniqueNumbers.add(2);
uniqueNumbers.add(1); // This value won't be added as it's a duplicate
    
Accessing Set Elements

Sets in Dart are unordered, which means they do not have a specific index for their elements. You can access elements in a set using iteration or by checking for the presence of a specific value.

Here’s how to access set elements in Dart:


var colors = {'red', 'green', 'blue'};
colors.contains('green'); // Check if 'green' is in the set
    
Modifying Sets

Sets in Dart are mutable, allowing you to add or remove elements. You can add new unique elements or remove existing ones from a set.

Here are examples of modifying sets:


var shapes = {'circle', 'square', 'triangle'};
shapes.add('rectangle'); // Adding a new unique element
shapes.remove('square'); // Removing an element
    
Set Methods

Dart provides a variety of built-in methods for working with sets. These methods make it easy to manipulate sets, including adding, removing, and checking for the presence of elements.

Here are some common set methods:


var numbers = {1, 2, 3};
numbers.add(4); // Add a new unique element
numbers.remove(2); // Remove an element
numbers.contains(3); // Check if an element exists in the set
numbers.length; // Get the number of elements in the set
    
Iterating Over Sets

To process each element in a set, you can use loops. Dart provides methods like forEach for iterating over the elements of a set.

Here’s how to iterate over a set in Dart:


var fruits = {'apple', 'banana', 'cherry'};
fruits.forEach((fruit) {
    print(fruit);
});
    
Set Operations

Sets support various set operations such as union, intersection, and difference. These operations allow you to combine or compare sets, providing powerful tools for solving problems involving multiple sets of data.

Here’s an example of set operations in Dart:


var A = {1, 2, 3};
var B = {3, 4, 5};

var union = A.union(B); // Union of sets A and B
var intersection = A.intersection(B); // Intersection of sets A and B
var difference = A.difference(B); // Set difference (A - B)
    
Conclusion

Sets are a fundamental data structure in Dart that allow you to store and manipulate unique values. Whether you’re working with distinct items or need to perform set operations, understanding how to use sets effectively is crucial for developing Dart applications. With various methods and techniques, you can efficiently manage and work with sets to meet your programming needs.