+ 1

How to swap two variables without using temp variable?

29th Jul 2017, 6:08 PM
Mohammad Hossein Meshkini
Mohammad Hossein Meshkini - avatar
9 Réponses
+ 3
a = a + b; b = a - b; a = a - b; You can check it with two values.
29th Jul 2017, 6:09 PM
Mohammad Hossein Meshkini
Mohammad Hossein Meshkini - avatar
+ 9
You can also use the XOR swapping algorithm. X := X XOR Y Y := Y XOR X X := X XOR Y
30th Jul 2017, 3:01 AM
Hatsy Rei
Hatsy Rei - avatar
30th Jul 2017, 6:26 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
https://code.sololearn.com/cRlRP41C5vzn/?ref=app
7th Oct 2017, 6:23 AM
David Ashton
David Ashton - avatar
0
a,b = b,a This can swap two variables
29th Jul 2017, 6:13 PM
Αητοιπe
Αητοιπe - avatar
0
I know that it works in javascript
29th Jul 2017, 6:18 PM
Αητοιπe
Αητοιπe - avatar
0
@antonie it works for ruby too
29th Jul 2017, 7:51 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar