Swap two numbers without temp variables

Algorithm to swap two numbers without using any extra temp variable.

Everything will be written in ES6.

Example

Input:
a = 1;
b = 2;

Output:
a = 2;
b = 1;

There are many ways we can solve this problem.

Using arithmetic operations

Implementation

  • We are going to subtract a from b and store it in b, b = b – a
  • Then we are going to add a and b and store it in a, a = a + b
  • And in the end we are going to subtract b from a and store it in b, b = a – b
//function to swap to numbers without any temp variable
 function swapNumbers(a, b){
   console.log('Before swapping a = '+a+' and b = '+b);
   b = b - a;
   a = a + b;
   b = a - b;
   console.log('After swapping a = '+a+' and b = '+b);
 }
Input:
swapNumbers(10, 15);

Output:
//How this works
/*Step 1*/ b = b - a = 15 - 10 = 5. /*So b = 5*/ 
/*Step 2*/ a = a + b = 10 + 5 = 15. /*So a = 15*/ 
/*Step 3*/ b = a - b = 15 - 5 = 10. /*So b = 10*/ 

Before swapping a = 10 and b = 15
After swapping a = 15 and b = 10

Time Complexity: O(1).
Space Complexity: O(1).

Time and Space complexity

  • We are solving this with only arithmetic operations, Time Complexity is O(1).
  • We are using only constant space, Space Complexity is O(1).

Using logical operations

Implementation

  • We are going to solve this problem using bitwise XOR, XOR returns 1 if two bits are different i.e 1 ^ 0 will return 1, same bits will return 0.
  • We are going to perform logical XOR on a and b and store it in a, a = a ^ b
  • Then we are going to perform XOR on a and b and store it in b, b = a ^ b
  • And in the end we are going to perform XOR on a and b and store it in a, a = a ^ b
//function to swap to numbers without any temp variable
 function swapNumbers(a, b){
   console.log('Before swapping a = '+a+' and b = '+b);
   a = a ^ b;
   b = a ^ b;
   a = a ^ b;
   console.log('After swapping a = '+a+' and b = '+b);
 }
Input:
swapNumbers(10, 15);

Output:
//How this works
/*Step 1*/ a = a ^ b = 00001111 ^ 00001010 = 00000101 = 5. /*So a = 5*/ 
/*Step 2*/ b = a ^ b = 00000101 ^ 00001010 = 00001111 = 15. /*So b = 15*/ 
/*Step 3*/ a = a ^ b = 00000101 ^ 00001111 = 00001010 = 10. /*So a = 10*/ 

Before swapping a = 10 and b = 15
After swapping a = 15 and b = 10

Time Complexity: O(1).
Space Complexity: O(1).

Time and Space complexity

  • We are solving this with only XOR, Time Complexity is O(1).
  • We are using only constant space, Space Complexity is O(1).
Note! bitwise operations in JavaScript are performed on 32bits binary numbers.

With Array Destructuring

We will use Destructuring to swap two numbers with using any temp variables.

//function to swap to numbers without any temp variable
 function swapNumbers(a, b){
   console.log('Before swapping a = '+a+' and b = '+b);
   [a, b] = [b, a];
   console.log('After swapping a = '+a+' and b = '+b);
 }
Input:
swapNumbers(10, 15);

Output:
Before swapping a = 10 and b = 15
After swapping a = 15 and b = 10

Time complexity: O(1).
Space complexity: O(1).

Time and Space complexity

  • We are using array Destructuring to swap numbers, so Time complexity is O(1).
  • We are using constant space, so Space complexity is O(1).

Leave a Reply

Your email address will not be published. Required fields are marked *