๐ŸŽฒ

JavaScript Sets

Store unique values and perform set operations

โญ Data Structure

What are Sets?

Sets are collections of unique values - no duplicates allowed! Perfect for finding unique ingredients across multiple recipes, removing duplicate items from shopping lists, or tracking which dishes a customer has ordered.

๐Ÿ’ก Real-World Example:

Remove duplicates from shopping list: ["eggs", "milk", "eggs", "bread"] โ†’ Set { "eggs", "milk", "bread" }

Working with Sets

1. Creating Sets

const ingredients = new Set();
ingredients.add("flour");
ingredients.add("sugar");
ingredients.add("flour");  // Duplicate - won't be added
console.log(ingredients.size);  // 2 (not 3!)

2. Creating from Arrays

const items = ["egg", "milk", "egg", "bread", "milk"];
const unique = new Set(items);
console.log(unique);  // Set { "egg", "milk", "bread" }
console.log([...unique]);  // Convert back to array

3. Checking and Deleting

const pantry = new Set(["flour", "sugar", "salt"]);
console.log(pantry.has("sugar"));  // true
pantry.delete("salt");
console.log(pantry.has("salt"));   // false

4. Iterating Over Sets

const dishes = new Set(["pasta", "pizza", "salad"]);
for (const dish of dishes) {
    console.log(dish);
}

5. Set Operations

const recipe1 = new Set(["flour", "sugar", "eggs"]);
const recipe2 = new Set(["eggs", "milk", "butter"]);

// Union (combine)
const all = new Set([...recipe1, ...recipe2]);

// Intersection (common items)
const common = [...recipe1].filter(x => recipe2.has(x));
console.log(common);  // ["eggs"]

โž• add(value)

set.add("item");
// Returns the Set

โœ… has(value)

set.has("item");
// Returns true or false

โŒ delete(value)

set.delete("item");
// Returns true if deleted

๐Ÿ—‘๏ธ clear()

set.clear();
// Removes all elements

Learn More

๐Ÿš€ Try It Yourself

Output:

๐Ÿ”

Game 1: Unique Ingredients Finder

Find all unique ingredients across multiple recipes! Practice Set operations like union, intersection, and difference.

๐Ÿ“ Recipe Lists

๐Ÿฐ Recipe A: Cake
๐Ÿช Recipe B: Cookies

๐Ÿ“Š Analysis Results

๐Ÿงน

Game 2: Duplicate Remover

Clean up messy ingredient lists by removing duplicates! Practice using Sets to ensure unique values.

๐Ÿ“‹ Input List (with duplicates)

โœจ Cleaned List (unique items)

๐Ÿ“Š Statistics:
โœจ

Challenge 1: Unique Ingredients Finder

๐Ÿ“‹ Your Mission:

Find all unique ingredients across multiple recipes:

  1. Combine all ingredient arrays into one
  2. Use a Set to get unique values
  3. Return sorted array of unique ingredients

Example:

findUniqueIngredients([["flour", "sugar"], ["sugar", "eggs"], ["flour"]])

โ†’ ["eggs", "flour", "sugar"] (sorted alphabetically)

๐Ÿ’ก Hints:

  • Flatten arrays: recipes.flat()
  • Create Set: new Set(array)
  • Convert back and sort: [...set].sort()

Test Results:

๐Ÿงน

Challenge 2: Duplicate Remover

๐Ÿ“‹ Your Mission:

Clean up a shopping list by removing duplicates and return statistics:

  1. Remove duplicate items from the list
  2. Count how many duplicates were removed
  3. Return object with cleaned list and duplicate count

Example:

removeDuplicates(["egg", "milk", "egg", "bread", "milk", "egg"])

โ†’ { cleanList: ["egg", "milk", "bread"], duplicatesRemoved: 3 }

๐Ÿ’ก Hints:

  • Original length - Set size = duplicates
  • Convert Set to array: [...set]
  • Return object with both values

Test Results: