3 ways to swap two Numbers without using Temp or Third Variable in Java? (original) (raw)

How to swap two numbers without using temp or third variable is a common interview question not just on Java interviews but also on C and C++ interviews. It is alsoagood programming question for freshers. This question was asked to me long back and didn't have any idea about how to approach this question without using temp or third variable, maybe lack of knowledge on bitwise operators in Java or maybe it didn't click at that time. Given some time and trial error, I eventually come out with a solution with just an arithmetic operator but the interviewer kept asking about other approaches of_swapping two variables without using temp or third variable_.

Personally, I liked this question and included in the list of myprogramming interview question because of its simplicity and some logical work, it forces you to do.

When I learned bit-wise operation in Java I eventually find another way of swapping two variables without a third variable, which I am going to share with you guys.

1. Swapping two numbers without using temp variable in Java

If you have ever heard this question, then you must be familiar with this approach of swapping numbers without using the temp variable.

If you are hearing this question the very first time, then try it yourself,it's agood programming exercise for an absolute first-timer.

By the way, here is the code example of swapping two numbers without using temp variable and using the arithmetic operator in Java:

int a = 10;

int b = 20;

System.out.println("value of a and b before swapping, a: " + a +" b: " + b);

// swapping value of two numbers without using temp variable

a = a+ b; // now a is 30 and b is 20

b = a -b; // now a is 30 but b is 10 (original value of a)

a = a -b; // now a is 20 and b is 10, numbers are swapped

System.out.println("value of a and b after swapping, a: " + a +" b: " + b);

Output:

value of a and b before swapping, a: 10 b: 20

value of a and b after swapping, a: 20 b: 10

2. Swapping two numbers without using temp variable in Java with the bitwise operator

Bitwise operators can also be used to swap two numbers without using a third variable. XOR bitwise operator returns zero if both operands are the same i.e. either 0 or 1 and return 1 if both operands are different e.g. one operand is zero and the other is one.

By leveraging this property, we can swap two numbers in Java.

Here is a code example of swapping two numbers without using temp variable in Java using XOR bitwise operand:

A B A^B (A XOR B)

0 0 0 (zero because operands are same)

0 1 1

1 0 1 (one because operands are different)

1 1 0

int a = 2; // 0010 in binary

int b = 4; // 0100 in binary

System.out.println("value of a and b before swapping, a: " + a +" b: " + b);

// swapping value of two numbers without using temp variable and XOR bitwise operator

a = a^b; // now a is 6 and b is 4

b = a^b; // now a is 6 but b is 2 (original value of a)

a = a^b; // now a is 4 and b is 2, numbers are swapped

System.out.println("value of a and b after swapping using XOR bitwise operation, a: " + a +" b: " + b);

value of a and b before swapping, a: 2 b: 4

value of a and b after swapping using XOR bitwise operation, a: 4 b: 2

And, here is a nice diagram to remember how to swap two numbers in Java and Python:

How to swap two integers in Java and Python

3. Swapping two numbers without using temp variable in Java with division and multiplication

swap two numbers without using thrid or temp variable in Java programmingThere is another, third way of swapping two numbers without using a third variable, which involves multiplication and division operator. This is similar to the first approach, where we have used + and - operators for swapping values of two numbers.

Here is the code example to swap two numbers without using a third variable with division and multiplication operators in Java :

int a = 6;

int b = 3;

System.out.println("value of a and b before swapping, a: " + a +" b: " + b);

// swapping value of two numbers without using temp variable using multiplication and division

a = a*b; // now a is 18 and b is 3

b = a/b; // now a is 18 but b is 6 (original value of a)

a = a/b; // now a is 3 and b is 6, numbers are swapped

System.out.println("value of a and b after swapping using multiplication and division, a: " + a +" b: " + b);

Output:

value of a and b before swapping, a: 6 b: 3

value of a and b after swapping using multiplication and division, a: 3 b: 6

That's all on3 ways to swap two variables without using a third variable in Java. Its good to know multiple ways of swapping two variables without using temp or third variable to handle any follow-up question.

Swapping numbers using a bitwise operator is the fastest among the three because it involves a bitwise operation.

It’s also a great way to show your knowledge of bitwise operators in Java and impress the interviewer, which then may ask some questions on a bitwise operation.

A nice trick to drive interviews in your expert area.

Other Coding Problems to learn Programming in Java

And, if you already solved this problem and looking for more challenging problems to really sharpen your coding and algorithm skills then you should try to solve problems given in the Algorithm Design Manual book by Steve Skiena. It's a great book to prepare for data structure and algorithms interview.

And lastly one question for you? What was the last coding question asked to you? If you remember more than one then feel free to share but at least share one question