Skip to curriculum
Jump to the editor

1.1. Swap two numbers without a temporary variable

Easy
Topics
5–15 min

Swapping two values is a step inside many algorithms: reversing an array, shuffling it, sorting it. The usual way uses a third variable to hold one value for a moment. This problem asks for three ways that need no third variable, and for you to see where each one stops working.

Write three functions in swap.js:

  • swapBySum(a, b) swaps two integers using only + and -.
  • swapByXor(a, b) swaps two 32-bit integers using only the bitwise XOR operator ^.
  • swapItems(list, i, j) swaps two items of an array in place with a destructuring assignment.

Signature

JAVASCRIPT
function swapBySum(a, b)      // returns [b, a]
function swapByXor(a, b)      // returns [b, a]
function swapItems(list, i, j) // returns list itself, changed
  • swapBySum and swapByXor change their own parameters a and b, then end with a return of the array of the two.
  • swapItems changes list and returns the same array, not a copy.

Requirements

  1. swapBySum returns the two values swapped, for positive and negative integers, zero, equal values and integers up to 2 ** 52.
  2. swapBySum uses only + and - on a and b: it declares no variable (let, const, var) and builds no array before its return.
  3. swapByXor returns the two values swapped, for any 32-bit integers, negative ones and zero included, and equal values.
  4. swapByXor uses only ^, with the same rule: no variable and no array before its return.
  5. swapItems swaps the items at i and j in the array passed in and returns that array.
  6. swapItems works for any order of i and j, for i === j, and for any kind of item, and leaves every other item and the length alone.

Examples

JAVASCRIPT
swapBySum(3, 7);    // [7, 3]
swapBySum(-4, 9);   // [9, -4]
JAVASCRIPT
swapByXor(12, 5);   // [5, 12]
swapByXor(-1, 77);  // [77, -1]
JAVASCRIPT
const list = ["a", "b", "c"];
swapItems(list, 2, 0);  // ["c", "b", "a"]
list;                   // ["c", "b", "a"]: the same array, changed
swapItems(list, 1, 1);  // ["c", "b", "a"]: nothing to swap

Environment

  • Plain JavaScript in a browser page; the tests call the globals swapBySum, swapByXor and swapItems from swap.js.
  • Two tests read the source of swapBySum and swapByXor, without comments, up to the last return. Keep the return as the last statement.
  • Floating-point numbers are not part of the tests for swapBySum, and numbers outside 32 bits are not part of the tests for swapByXor: both are explained in the solution.

Hints

  1. After a = a + b, the variable a holds both numbers at once. How do you get the old a back out of it?
  2. What is x ^ x? What is x ^ 0?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…