0Pricing
Mojo Academy · Lesson

Unique Items with Set

Track membership without duplicates.

Unique Items with Set is a free Mojo Academy lesson on CoddyKit — lesson 3 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 Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Track Uniqueness

When you only care whether something is present, not how many times, use a Set. It holds each item once. ✨

One Element Type

A Set takes a single element type in brackets, like Set[Int]. Every member shares that type.

var ids = Set[Int]()

Add a Member

Insert a value with add. Adding something already present simply does nothing, keeping items unique.

ids.add(7)
ids.add(7)

Duplicates Collapse

Because membership is unique, adding 7 twice leaves just one 7. A set dedupes for you automatically.

Check Membership Fast

Test presence with the in operator. This is a set core strength: answering belongs-or-not very quickly.

if 7 in ids:
    print("present")

Count the Members

Use len to see how many unique items a set holds. Duplicates never inflate this count.

var size = len(ids)

Remove a Member

Take an item out with remove. After removal the set no longer reports that value as present.

ids.remove(7)

Iterate the Set

You can loop a set to visit each member, but the order is not guaranteed. Sets care about membership, not sequence.

for x in ids:
    print(x)

Union of Two Sets

Combine sets with union to get every item from both, still with no duplicates.

var both = a.union(b)

Intersection of Two Sets

Find shared items with intersection. It keeps only the values present in both sets.

var common = a.intersection(b)

When a Set Shines

Reach for a set to remove duplicates or test membership repeatedly. It beats scanning a list every time.

Quick Check

You add the value 7 to an empty Set, then add 7 again. What is len of the set?

Recap

A Set stores unique items for fast membership tests. Use add and remove, in to check, and union or intersection to combine. 🎯

Frequently asked questions

Is the “Unique Items with Set” lesson free?

Yes — the full text of “Unique Items with Set” is free to read here on the web, and the Mojo 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 Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Unique Items with Set”?

Track membership without duplicates. You practise Mojo 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 Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Unique Items with Set” 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 Mojo Academy lesson?

Yes. Every Mojo 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

  1. Building and Growing a List
  2. Key-Value Data with Dict
  3. Unique Items with Set
  4. Choosing the Right Collection
← Back to Mojo Academy