Back to problems

Swap Two Variables Without Using Temporary Variable

Algorithm · Apple · Easy

Implement a method that exchanges the values of two provided arguments without allocating an auxiliary variable. The arguments may be primitive types or reference types that support arithmetic operations such as addition and subtraction. Your solution must operate in place — after the swap, the first argument should hold the value initially held by the second, and the second should hold the value initially held by the first. Example 1: Explanation: The values are exchanged…

Checking your access…