How do you swap the values of 2 variables without creating a third temporary variable? There really isn’t a practical reason to do this, other than having some uninformed employer who thinks this is a good question to ask a candidate. So before I explain the technique, here are some reasons to not do this in a real application:
You can find lots of examples of doing this online, but here’s why it works:
Let’s take 2 variables:
x = 5
y = 10
Now consider some basic math:
x + y = z, which becomes 5 + 10 = 15
As long as you have any 2 of those 3 values, you can calculate the 3rd.
z = x + y
x = z – y If we lose x, we can still calculate it’s original value from z and y.
y = z – x If we lose y, we can still calculate it’s original value from z and x.
So now we have the following sequence to swap the values:
x = x + y, giving us x equal to 15, and y is still equal to 10. The difference is 5, our original value of x, so we can still get our original values.
y = x – y, therefore y equals 5 (15 – 10), the original value of x.
x = x – y, and now x becomes 15 – 5 = 10, the original value of y.
I used +/- here, but we can do the same with multiplication:
x = x * y, now x = 50 and y = 10
y = x / y, so now y = 5 and x = 50
x = x / y, so now x = 10 and y = 5
We can also use XOR:
x = x ^ y, and now x = 15
y = x ^ y, and now y = 5
x = x ^ y, and now x = 10
One of the problems with the mathematical techniques is overflow problems. If your values go over the limit of what your number type can hold, weird things happen. C# won’t always throw an error but you’ll get the wrong results.
int x = 1024
int y = 2147483647
Using the multiplication method, we end up with a “Divide by 0” error.
int x = 2147483647
int y = 1024
Again using multiplication, we end up with:
x = 1024
y = -1
No error, but definitely not the result we wanted.
Now, what if we have strings instead of numbers? Basically, use a temp variable:
x = “Hello”
y = “World”
z = x
x = y
y = z
Attempting to swap 2 strings without a temporary variable is messy, overly complicated, and prone to error. For example, the following seems reasonable:
x = x + y, now x = “HelloWorld”
y = x.Substring(0, x.IndexOf(y)), and y = “Hello”
x = x.Substring(y.Length), and now x = “World”
That works! Well, with our test data of “Hello” and “World”. Trying this against “mxyzptlk” and “tlk”, and we end up with:
x = “tlktlk”
y = “mxyzp”
Now poor Mr. Mxyzptlk has had his name mangled. As soon as we have one string that contains the second, our swap fails. A slight change gives us:
x = x + y, now x = “mxyzptlktlk”
y = x.Substring(0, x.Length – y.Length), and y = “mxyzptlk”
x = x.Substring(y.Length), and now x = “tlk”
Much better, but because strings are immutable, this code keeps creating new strings.
More important, can you tell from a quick glance what that code is doing?
Using a temporary variable is simple, fast, and easily understood. So do it.
I am a computer programmer specializing in building database software with ASP.NET MVC, C#, and SQL Server.