Why does it output { 7, 8, 9} out of order, but instead { 8, 9, 7}? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

22nd Sep 2023, 3:35 PM
Хорошо
Хорошо - avatar
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..
22nd Sep 2023, 4:22 PM
Dragon RB
Dragon RB - avatar
+ 2
Хорошо Dragon RB is right! The set differece operation creates a new set, and the only important thing is the elements are there. The order is totaly irrellevant.
22nd Sep 2023, 5:44 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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.
22nd Sep 2023, 6:27 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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.
22nd Sep 2023, 6:02 PM
Хорошо
Хорошо - avatar