Sets

A Set is an unordered collection that stores only unique elements and offers fast membership checks, making it the right tool whenever duplicates must be rejected or "have I seen this?" must be answered quickly.

Learn Sets in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll build sets, add elements, test membership, and combine them with set algebra — union | , intersection & , difference - — then dedupe arrays the efficient way.

What You'll Learn in This Lesson

1️⃣ Creating Sets & Membership

After require 'set' , build one with Set.new(array) — duplicates are dropped automatically. Add elements with add or its alias << , and test membership with include? , which is far faster than scanning an array.

2️⃣ Set Algebra

Sets support the operators from math class: | for union, & for intersection, - for difference, and ^ for symmetric difference. Each returns a new set, so the originals are untouched.

3️⃣ Subsets & Deduping

subset? and superset? answer containment questions, and converting an array with to_set deduplicates it. When you only need a one-off dedup back as an array, Array#uniq is fine; a Set wins when you'll repeatedly check membership.

Your turn. Fill in each ___ blank with the right operator, then run it.

Use two sets — one for words you've seen, one for duplicates — to find which words appear more than once. Run with ruby repeats.rb .

📋 Quick Reference — Sets

Practice quiz

What is the defining property of a Set?

  • It keeps insertion order
  • It allows duplicates
  • It stores only unique elements
  • It is always sorted

Answer: It stores only unique elements. A Set is an unordered collection that automatically rejects duplicate elements.

What does Set.new([1, 2, 2, 3, 3, 3]).size return?

  • 3
  • 6
  • 2
  • 1

Answer: 3. Duplicates vanish, leaving the unique elements {1, 2, 3}, so size is 3.

Which method tests membership in a Set?

  • has?
  • member
  • contains
  • include?

Answer: include?. include? checks membership and is effectively O(1) thanks to a hash internally.

Which operator computes the UNION of two sets?

  • &
  • |
  • -
  • ^

Answer: |. | is union — every element that is in either set.

Which operator computes the INTERSECTION of two sets?

  • &
  • |
  • -
  • +

Answer: &. & is intersection — only elements present in both sets.

Given a = Set[1,2,3,4] and b = Set[3,4,5,6], what is a - b?

  • {3, 4}
  • {5, 6}
  • {1, 2}
  • {1, 2, 5, 6}

Answer: {1, 2}. Difference a - b is elements in a but not b, which is {1, 2}.

What does the alias << do on a Set?

  • Removes an element
  • Adds an element (same as add)
  • Computes union
  • Sorts the set

Answer: Adds an element (same as add). << is an alias for add — it inserts one element.

Adding an element that is already present to a Set...

  • Raises an error
  • Duplicates it
  • Clears the set
  • Is a harmless no-op

Answer: Is a harmless no-op. Adding an existing element does nothing — the set never grows beyond its unique members.

Which method returns true when every element of small is also in big?

  • small.superset?(big)
  • small.subset?(big)
  • small.member?(big)
  • small.union?(big)

Answer: small.subset?(big). subset? is true when every element of the receiver is contained in the argument set.

What is the fastest way to dedupe an array and keep doing membership checks?

  • Repeated Array#include?
  • Sort it first
  • Convert it to a Set with to_set
  • Use a nested loop

Answer: Convert it to a Set with to_set. to_set gives O(1) include? checks, far faster than scanning an array repeatedly.