Junior — Middle
How can duplication of elements be prevented when inserting into a collection of type Set?
sobes.tech AI
Answer from AI
In collections like Set, duplication of elements is automatically prevented because Set only stores unique values. When attempting to add an element that already exists in the set, it will not be added again.
Example in Python:
my_set = {1, 2, 3}
my_set.add(2) # element 2 already exists, the set will not change
print(my_set) # Output: {1, 2, 3}
Therefore, to avoid duplication, it is sufficient to use Set instead of a list or another collection that allows repetitions.