luthfisauqi17

Posted on

# Swap Values of Two Variables Without Any Helper Variable

When you are doing a programming interview or during a programming test, have you ever been asked to create a code to swap values between two variables? If yes, how do you do it?

The most classic and famous way to perform this operation is to use the help of a helper variable to serve as a temporary shelter for the value of one of the two variables.

Below is the code that implements this method.

Source code 1:

``````public static void main(String[] args) {
int a = 4;
int b = 5;
int temp;

temp = a;
a = b;
b = temp;

System.out.println("Variable a: " + a);
System.out.println("Variable b: " + b);
}
``````

In Source code 1, we can see that there is a helper variable `temp` which functions as a temporary store of the value of the variable `a`.

Output 1:

``````Variable a: 5
Variable b: 4
``````

In Output 1, we can see that the variable `a` is already occupied by the previous value of the variable `b`.

But there is one cool math trick you can use to swap the values of two variables, which in this trick you don't even need any helper variables. The trick is that we will perform addition and subtraction operations in the variables that we will swap.

The way it works is as follows:

1. Imagine we have a variable `a` with a value of 4, and a variable `b` with a value of 5.
2. We will add the variable `a` with the variable `b`, and store it in the variable `a`, so that now the variable `a` has a value of 9.
3. Next, we will reduce the variable `a` with the variable `b` and store it in the variable `b`, so that the value of the variable `b` becomes 4.
4. And finally, we will subtract variable `a` from variable `b` and store it in variable `a`, so the current value of variable `a` is 5.

For more clarity, you can see Source code 2, as an implementation of this method.

Source code 2:

``````public static void main(String[] args) {
int a = 4;
int b = 5;

a = a + b;
b = a - b;
a = a - b;

System.out.println("Variable a: " + a);
System.out.println("Variable b: " + b);
}
``````

In Source code 2, you can see that there are no helper variables to help with this swap process.

Output 2:

``````Variable a: 5
Variable b: 4
``````

In Output 2, we can see that the results of implementing Source code 2 match our expectations that the values of the variable `a` and the variable `b` are swapped.

Congratulations, you've learned this simple and cool trick that you might be able to use to impress your interviewer or your lecturer.

Cover image:

https://i.picsum.photos/id/962/1920/720.jpg?hmac=oN1aUzZuuUM6aR1lRB91s6Y8GYZQ1ZEmpIY-c8NL_h8

Other image:

https://cdn-images-1.medium.com/max/2560/1*mQ2zfd2fN75igCOeW9iWrw.jpeg

Aaron Reese

a,b=b,a

luthfisauqi17

I remember this from Python, such a cool feature. I hope this feature will be in Java as well. Thank you for the response.

Oleksandr

Considering gc nature of java, it could be faster than using TMP var. It would be interesting to see some benchmarks :)

luthfisauqi17

I agree with you. You know, you just gave me an idea, looks like in the future I'll be doing a performance analysis between swap techniques that use extra variables and those that don't. Thanks for the idea :)

thecodingcrow

XOR swap also works well here

a = a ^ b;
b = a ^ b;
a = a ^ b;

luthfisauqi17

Yes, that way works too. Very cool, thank you.

In C++, `std::swap(a, b);`