How to Swap Two Numbers without Temp or Third variable in Java?

How to swap two numbers without using a third variable is very common interview question. It can be asked in a campus interview or even for a Senior position. This question was first asked to me in my college days. When I was well prepared with all the approaches. But surprisingly again I faced the same question on an interview when I was 5 – 6 years experienced. And this time it took some time for me to recollect my college learnings to implement it. I was able to recollect 1 of the approaches using arithmetic operator (+,-).  But the interviewer was keep asking about other approaches of swapping two variables without using a third variable.

So, be prepared with this Question as I found it as an ever-green beauty in interview Questions. 

First we will see the arithmetic operator (+,-) approach which I was able to recollect in my second interaction with this question. Consider we have 2 int values as :

int a = 15;
int b = 20;

// to swap the values as b = 15 and a = 20 we need to perform the following operations
a = a + b; //now a is 35 and b is 20
b = a - b; //now b is 15 a is 35 (b is swapped)
a = a - b; //now a is 20 and b is 15, both numbers are swapped

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

Bitwise operators can also be used to swap two numbers without using third variable. X-OR bitwise operator returns zero if both operand is same i.e. either  0 or 1 and returns 1 if both operands are different e.g. one operand is zero and other is one. To swap 2 number using X-OR we need to perform the following operations. Lets check the XOR truth table for a clear understanding of XOR operation.

X-OR Table :

  A    B   A X-OR B
1 1 0
1 0 1
0 1 1
0 0 0
a = a ^ b
b = a ^ b
a = a ^ b 

Let a = 12 and b = 9 [ For simplicity consider number is of 4 bits ]
a = 1100
b = 1001

a = a ^ b = 1100 ^ 1001 = 0101
b = a ^ b = 0101 ^ 1001 = 1100 // (b is swapped)
a = a ^ b = 0101 ^ 1100 = 1001 // (both numbers are swapped)

Swapping two numbers with division and multiplication

There is another, third way of swapping two numbers without using third variable, which involves multiplication and division operator. This is similar to first approach, where we have used + and - operator for swapping values of two numbers. Here is the code example to swap tow number without using third variable with division and multiplication operators in Java :

int a = 15;
int b = 20;

//swapping value of two numbers without using temp variable using multiplication and division
a = a * b; //now a is 300 and b is 20
b = a / b; //now a is 300 but b is 15 (b is swapped)
a = a / b; //now a is 20 and b is 15, both numbers are swapped

Now consider the following implementation of all 3 approaches:

public class SwapNumbers {

	public static void swapByBitWiseOperator(int a, int b) {
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		System.out.println("After BitWise Swap :");
		System.out.println("a :" + a + " and b : " + b);
	}

	public static void swapByBitPlusAndMinus(int a, int b) {
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("After PlusAndMinus Swap :");
		System.out.println("a :" + a + " and b : " + b);
	}

	public static void swapByMultiplicationAndDivision(int a, int b) {
		a = a * b;
		b = a / b;
		a = a / b;
		System.out.println("After MultiplicationAndDivision Swap :");
		System.out.println("a :" + a + " and b : " + b);
	}

	public static void main(String[] args) {
		int a = 15, b = 10;
		System.out.println("Before BitWise Swap :");
		System.out.println("a :" + a + " and b : " + b);
		swapByBitWiseOperator(a, b);
		System.out.println();
		a = 45;
		b = 40;
		System.out.println("Before PlusAndMinus Swap :");
		System.out.println("a :" + a + " and b : " + b);
		swapByBitPlusAndMinus(a, b);
		System.out.println();
		a = 5;
		b = 60;
		System.out.println("Before MultiplicationAndDivision Swap :");
		System.out.println("a :" + a + " and b : " + b);
		swapByMultiplicationAndDivision(a, b);
	}
}

The output of the program is :

Before BitWise Swap :
a :15 and b : 10
After BitWise Swap :
a :10 and b : 15

Before PlusAndMinus Swap :
a :45 and b : 40
After PlusAndMinus Swap :
a :40 and b : 45

Before MultiplicationAndDivision Swap :
a :5 and b : 60
After MultiplicationAndDivision Swap :
a :60 and b : 5

These are the ways of swaping variable without using 3rd variable.
 

core java 12 Algorithm 12

FOLLOW US ON LinkedIn



Explore Tutu'rself