I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
Also worth adding that you might want to stop once you have a set and not convert it back to an array.
Why?
Lookup time from a set is constant to check if a value is in a set while in an array of 1000 elements worst case you have to go through 1000 elements until you find the one at the end.
Secondly when you convert from array to set, the order will be lost. Sets are inherently unordered. So when you convert from set to array you will not get the order of the original array. So you might as well keep the set.
I'm a self-taught dev focused on websites and Python development.
My friends call me the "Data Genie".
When I get bored, I find tech to read about, write about and build things with.
“It’s only work if somebody makes you do it.” • craft code • creative ideas • cutting edge • author • senior front end architect • professional scuba diver • adventures above and below the sea level
Location
Germany
Work
Senior Front End Architect, Full Stack Engineer, Creative Technologist and Scuba Diving Professional
Worth explaining this separately when used with array instead of sets
And modifying x and y won't affect the other.
Then bringing in set
And the the original post does that in one line in a function
Also worth adding that you might want to stop once you have a set and not convert it back to an array.
Why?
Lookup time from a set is constant to check if a value is in a set while in an array of 1000 elements worst case you have to go through 1000 elements until you find the one at the end.
Secondly when you convert from array to set, the order will be lost. Sets are inherently unordered. So when you convert from set to array you will not get the order of the original array. So you might as well keep the set.
It's actually specced to preserve insertion order: developer.mozilla.org/en-US/docs/W...
(
...
spread operator callsy[Symbol.iterator]
, which is specced to be the same as what is returned by.values()
onSet
)Oh interesting, in Python it is unordered.
Thank you for the explanation.