set is unordered collection. So for like http accept language header where the order matters (i.e. the first language value is what we want first when we iterate), we shouldnt use a set. Even when the lookup time for set is o(1), it doesnt fit the purpose.
You wrote visited = set(start). In Python, set("USD")
actually creates a set of characters: {'U', 'S', 'D'}.
This will cause the BFS to skip nodes incorrectly.
Fix: Use visited = {start}.
so correct way is like this
visited = {start}
Finding common elements: The intersection (&) gives you the elements present in both set1 and set2. (intersection) This can be useful for identifying shared characteristics or items.
in java we use .retainAll()
// Create a new set to store the intersection (to avoid modifying set1)
Set<Integer> intersection = new HashSet<>(set1);
// The retainAll() method modifies the 'intersection' set,
// keeping only the elements that are also present in set2.
intersection.retainAll(set2);
Combining unique elements: The union (|) gives you all the unique elements from both sets. This can be helpful when you need a collection of all distinct items.
we use .addAll()
// Create a new set to store the union (or modify one of the originals)
Set<Integer> unionSet = new HashSet<>(set1); // Start with the elements of set1
// Add all elements from set2 to unionSet.
// If an element is already present, it won't be added again
// because Sets only store unique elements.
unionSet.addAll(set2);
System.out.println("Set 1: " + set1);
System.out.println("Set 2: " + set2);
System.out.println("Union (set1 | set2): " + unionSet);
Finding distinct elements: The difference (-) helps you find elements that are in one set but not the other. This can be useful for identifying unique items in a particular group.
Set<Integer> union = new HashSet<>(set1);
union.addAll(set2);
Set<Integer> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
Set<Integer> symmetricDifference = new HashSet<>(union);
symmetricDifference.removeAll(intersection);
Finding elements unique to each set: The symmetric difference (^) gives you elements that are in either set1 or set2, but not in both. This highlights the items that are exclusive to each set.
in py its much easier
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# 1. Symmetric Difference (Exclusive to each set)
# Result: {1, 2, 4, 5} (3 is removed because it's in both)
exclusive = set1 ^ set2
# 2. Intersection (Common elements)
# Result: {3}
common = set1 & set2
# 3. Union (All unique elements)
# Result: {1, 2, 3, 4, 5}
all_unique = set1 | set2
# 4. Difference (In set1 but NOT set2)
# Result: {1, 2}
only_in_1 = set1 - set2
for union it is set1 | set2
for set comprehension look at
https://velog.io/@whitehousechef/Leetcode-898.-Bitwise-ORs-of-Subarrays
for that above link, The update() method adds elements from another iterable (like a list, set, tuple, or string) into the original set. It does not return a new set; it modifies the set in-place.
we cant access elements tored in set by index but if we convert to list, we can get the value through the index and remove that value in the set
my_set = {1, 2, 3, 4}
my_list = list(my_set)
print(my_list)