DEV Community

Discussion on: How can you swap two variables without using a third?

Collapse
 
theoutlander profile image
Nick Karnik

You can do anything as long as you swap the values and don't create more memory.

In your solution, x is overwritten by y so it isn't swapping both the values.

Collapse
 
phucduong86 profile image
Phuc Duong

Ah, I thought it wouldn't work but at midnight computer tells you lies :).

The multiple assignment in Go ( assuming C/C++ are the same mechanism, and i see Python below) works though.

x, y = y,x