Dart Sets for Beginners: The Unique Collection (No Duplicates Allowed!)

Dart Sets

We’re going to dive into the world of Sets in Dart. Think of a Set as the most organized, no-nonsense toy collector in the whole world!

1. The Very Neat Toy Bin (What is a Dart Sets?)

Imagine you have a big, colorful toy bin.

  • In a List (the first one we talked about), you could have a red car, then another red car, and then a third red car. The order matters, and duplicates are fine.
  • In a Set, things are very different. The Set is a special bin that has one, very important rule:

The Set Rule: Every single item must be unique. No duplicates allowed!

If you try to put a second red car into the Set bin, the Set will just look at it and say, “Nope! I already have one of those,” and it will ignore the duplicate.

The other rule is:

The Set Rule 2: The order doesn’t matter.

It’s just a big pile of unique things. You can’t ask, “What is the first item?” You can only ask, “Is the blue teddy bear in the bin?”

2. Making the Set (Creating a Set)

Just like a Map, we use curly braces {} to make a Set. But because there are no Name Tags (Keys) like in a Map, we only put the things (Values) inside.

What we want to storeDart Code
A collection of unique colorsvar colors = {'red', 'blue', 'green'};
A collection of unique numbersvar luckyNumbers = {5, 10, 15};

The “No Duplicates” Magic Trick

Watch what happens when we try to put duplicates in:

var fruits = {'apple', 'banana', 'apple', 'orange', 'banana'};

print(fruits); // Output: {apple, banana, orange}

Even though we typed ‘apple’ twice and ‘banana’ twice, the Set quietly and magically kept only one of each! This is the main superpower of a Dart Set.

Dart Sets

3. Playing with the Set (Modifying and Checking)

Since there are no numbers (indices) or Name Tags (keys), how do we work with the items? We talk to the set by the item’s name!

What you want to doDart Code
Add a new unique item.fruits.add('grape');
Ask if an item is there.fruits.contains('apple'); (This will say true or false!)
Take out an item.fruits.remove('banana');
Count how many unique items are there.print(fruits.length);

4. The Super Set Games (Special Powers)

This is where the Set gets truly fun. Imagine you have two separate toy bins (two different Sets). You can play special math games with them!

Let’s use two sets of names:

var kidsInRoomA = {'Sam', 'Mia', 'Tom'};
var kidsInRoomB = {'Mia', 'Zoe', 'Max'};
Game NameWhat it findsDart CodeResult
Union (The “All Together” Game)Puts ALL the kids from both rooms together, but only one of each name!RoomA.union(RoomB);{'Sam', 'Mia', 'Tom', 'Zoe', 'Max'}
Intersection (The “Shared Friends” Game)Finds only the kids who are in both Room A AND Room B.RoomA.intersection(RoomB);{'Mia'}
Difference (The “Only In A” Game)Finds the kids who are only in Room A, but NOT in Room B.RoomA.difference(RoomB);{'Sam', 'Tom'}

This is why Sets are so helpful! They are perfect for when you need to quickly check if an item is already in a group, or when you need to combine groups and automatically throw away all the duplicates.

Leave a Reply

Your email address will not be published. Required fields are marked *