Skip to content

itmepavan/6-Program-python-and-Java-code-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

βœ… Programming Basics – Part 6: Sets πŸ“šπŸ§ 

βœ… What is a Set?
A set is a collection of unique, unordered elements. It doesn’t allow duplicates.

➊ How to Create a Set

πŸ“ Python

fruits = {"apple", "banana", "cherry"}

πŸ“ Java

Set<String> fruits = new HashSet<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");

βž‹ Properties of Sets
βœ”οΈ No duplicate values
βœ”οΈ No guaranteed order
βœ”οΈ Fast membership tests (in, contains)

➌ Add Elements

πŸ“ Python: fruits.add("mango")
πŸ“ Java: fruits.add("mango");

➍ Remove Elements

πŸ“ Python: fruits.remove("banana")
πŸ“ Java: fruits.remove("banana");

➎ Check Membership

πŸ“ Python: "apple" in fruits
πŸ“ Java: fruits.contains("apple")

➏ Loop Through a Set

πŸ“ Python:

for fruit in fruits:
    print(fruit)

πŸ“ Java:

for(String fruit : fruits) {
    System.out.println(fruit);
}

➐ Why Use Sets?

  • Eliminate duplicates from data
  • Fast checks for existence
  • Efficient in set operations (union, intersection)

βž‘ Real-World Uses of Sets

  • Store unique tags or categories
  • Track visited pages
  • Remove duplicates from a list
  • Fast user lookup in access control systoperations fast!*

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published