DEV Community

Discussion on: An Algorithm to Swap Two Variables Synchronously, Without Creating a Temporary Variable

Collapse
 
romanright profile image
Roman Right

Hello,
Nice article and correct algo. But while you are using python here, there is a trick about this. Python supports tuple assignments. it means you can swap the values with just:

a, b = b, a
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ninofiliu profile image
Nino Filiu

js too

[a, b] = [b, a]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
goodevilgenius profile image
Dan Jones

PHP, too.

[$a, $b] = [$b, $a];
Enter fullscreen mode Exit fullscreen mode