5 Best Ways to Swap Two Variables in One Line in C, C++, Python, PHP, and Java

πŸ’‘ Problem Formulation: Swapping two variables is a common task in programming that involves interchanging the values stored in the variables. For example, if variable a contains the value 10 and variable b contains the value 20, then after swapping, a should hold the value 20 and b the value 10. This article describes efficient one-liners that swap two variables in five different programming languages.

Method 1: Swap Using Tuple in Python

The Python language allows swapping variables in a single line by leveraging the tuple unpacking feature. This method is concise and Pythonic, utilizing the capability of tuples to store multiple values and unpack them back into variables.

Here’s an example:

a, b = 10, 20
a, b = b, a
print(a, b)

The output of this code snippet will be:

20 10

This example demonstrates the simplicity of swapping two variables in Python. The line a, b = b, a uses tuple packing and unpacking – b, a creates a tuple, and a, b unpacks it immediately into the two variables, hence swapping their values without the need for a temporary variable.

Method 2: Swap Using XOR Operator in C/C++

In C and C++, the XOR bitwise operator can be used to swap two variables in one line without using a temporary variable. This approach capitalizes on the properties of XOR, which makes it an interesting bitwise hacking technique.

Here’s an example:

int a = 10, b = 20;
a ^= b ^= a ^= b;

The output of the corresponding code to print the values would be:

20 10

This line of code utilizes a sequence of XOR operations to swap the values. However, it’s worth noting that modern compilers may generate warnings or incorrect results for this type of expression due to potential undefined behavior, so while it’s a neat trick, it’s not recommended for practical use.

Method 3: Swap Using List Assignments in PHP

PHP supports list assignments, which can be used to swap variables. This feature is similar to Python’s tuple unpacking, providing a convenient and readable one-liner for swapping values.

Here’s an example:

$a = 10;
$b = 20;
list($a, $b) = array($b, $a);
echo "$a $b";

The output of the code will be:

20 10

This code snippet employs PHP’s list() function to unpack an array into the list of variables on the left-hand side, effectively swapping them. It’s as simple as destructuring an array, and it’s easy to read and understand.

Method 4: Swap Using Parallel Assignment in Java

Java does not have built-in tuple types like Python but can perform swapping in a single statement by combining assignments. While this is less concise compared to languages with tuple support, it still allows swapping without a temporary variable.

Here’s an example:

int a = 10, b = 20;
a = (b = (a + b) - a) - b = a;
System.out.println(a + " " + b);

And the output will be:

20 10

This complex single line of code makes use of compound assignment operations to swap two variables. It is a mathematical trick that might be more challenging to understand at a glance and thus generally not recommended for code readability.

Bonus One-Liner Method 5: Swap Using the Spread Operator in JavaScript

Although not part of the initial list, JavaScript’s spread operator provides a neat way to swap variables reminiscent of Python’s tuple packing and unpacking. This ECMAScript 6 feature makes for concise and clean code.

Here’s an example:

let a = 10, b = 20;
[a, b] = [b, a];
console.log(a, b);

Output:

20 10

This one-liner leverages the array destructuring assignment that the spread operator enables within JavaScript, offering a very human-readable way to swap the values of two variables.

Summary/Discussion

  • Method 1: Tuple Unpacking in Python. Strengths: Simple, readable, pythonic. Weaknesses: Specific to Python.
  • Method 2: XOR Operator in C/C++. Strengths: No extra memory for temporary variable. Weaknesses: Potentially undefined behavior, difficult to read.
  • Method 3: List Assignment in PHP. Strengths: Simple syntax, easy to understand. Weaknesses: More verbose than Python’s equivalent.
  • Method 4: Parallel Assignment in Java. Strengths: Possible without extra variables. Weaknesses: Complicated, poor readability.
  • Bonus Method 5: Spread Operator in JavaScript. Strengths: Clean and modern syntax. Weaknesses: Only available in newer JavaScript versions (ES6 and above).