Set Operations: union, intersection, difference
Combine and compare sets.
Set Operations: union, intersection, difference is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Sets Do Math
Sets borrow ideas from math. With three small methods you can combine or compare them like circles in a Venn diagram. 🔵
Meet union
The union method merges two sets into one. Every item from both appears, and duplicates collapse to a single copy.
var a = {1, 2};
var b = {2, 3};
print(a.union(b));Union Keeps It Unique
Even when both sets share values, union still returns a set, so the result never holds repeats.
var r = {1, 2}.union({2, 2, 3});Meet intersection
The intersection method keeps only items found in both sets. It answers what do these two have in common.
var a = {1, 2, 3};
var b = {2, 3, 4};
print(a.intersection(b));No Overlap, Empty Result
If two sets share nothing, intersection returns an empty set rather than throwing an error.
var r = {1, 2}.intersection({3, 4});Meet difference
The difference method returns items in the first set that are not in the second. Think of it as subtraction.
var a = {1, 2, 3};
var b = {2};
print(a.difference(b));Order Matters Here
Unlike union, difference is not symmetric. Swapping the two sets gives you a different result.
var x = {1, 2}.difference({2, 3});Results Are New Sets
Each of these methods builds and returns a fresh Set. Your original sets stay exactly as they were.
Checking a Subset
Use containsAll to ask if one set fully fits inside another. It returns true only when every item is present.
var big = {1, 2, 3};
print(big.containsAll({1, 2}));Chaining Operations
Because each call returns a set, you can chain operations together to express richer logic in one line.
var r = {1, 2, 3}.union({4}).difference({1});A Real Use Case
Comparing two tag lists? intersection finds shared tags while difference reveals what only one side has.
var shared = {'a', 'b'}.intersection({'b', 'c'});Quick Check
Which method returns only the items that two sets share?
Recap
Well done! Use union to merge, intersection to find common items, and difference to subtract. Set math keeps your code clean. 🎯
Frequently asked questions
Is the “Set Operations: union, intersection, difference” lesson free?
Yes — the full text of “Set Operations: union, intersection, difference” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Set Operations: union, intersection, difference”?
Combine and compare sets. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Set Operations: union, intersection, difference” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sets and Guaranteed Uniqueness
- Set Operations: union, intersection, difference
- Maps as Key-Value Stores
- Iterating Entries, Keys, and Values