+ 1

# Why does it output { 7, 8, 9} out of order, but instead { 8, 9, 7}?

Not clear moment. https://code.sololearn.com/cJmLj4MrZH96/?ref=app

4 Answers

+ 4

Хорошо
I'm not sure, but..,
Sets are designed to be unordered, which means order doesn't matter..
If you want it to be ordered, you can use the OrderedSet class, but you'll need to install it..

+ 2

Хорошо
Every element in a set has a hash code that determines its position. If a new element’s hash code collides with an existing one, it’s assigned a new position, which can affect the order of elements. Additionally, memory allocation and optimization for efficiency can influence the order of elements.
However, sets have other advantages, such as being extremely fast at checking for membership. The time complexity for this operation is O(1), compared to O(n) for lists.
It’s like choosing between boiling all your eggs at once or boiling them one at a time for breakfast. I think we know which most people would prefer.

+ 1

Thank you, I was wondering how exactly the set is generated. After all, if we increase the number of elements in the second set, we will arrive at a set ordered in ascending order. Is there any factor that causes automatic ordering.