DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

Create a Duplicate of an Array

Reading · 4 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

A duplicate needs separate storage for the same four values

Start with the source array [4, 2, 7, 1]. Its length is n = 4, so its valid indices are 0 through 3. A duplicate needs another array with length 4, not just another name for the source. Success means that destination[0] through destination[3] contain 4, 2, 7, and 1 in the same order, while the destination occupies storage separate from the source.

The destination can begin with four reserved cells: [_, _, _, _]. The underscore means that no copied value has been placed there yet. The source already owns its four cells, and the destination owns four different cells. Matching values and matching indices describe what the arrays contain, not whether they share storage.

the source array [4, 2, 7, 1] and its separate destination array of length 4Two separate four-cell arrays are aligned under index labels 0, 1, 2, and 3. The source row contains 4, 2, 7, and 1. The destination row has one distinct cell for each matching index, and each source cell connects to the destination cell directly below it. Separate outer boundaries show that the arrays occupy different storage.4271same indexsourcedestination01230123matching indices do not mean shared storage
Matching values and indices do not require shared storage.

Copying source[i] into destination[i] preserves every position

Traverse the source with a loop index i. On each iteration, read the value at source[i] and put it into the destination cell with the same index. Starting with an empty destination, i = 0 produces [4, _, _, _], i = 1 produces [4, 2, _, _], i = 2 produces [4, 2, 7, _], and i = 3 produces [4, 2, 7, 1].

CPPEach source position is copied to the destination position with the same index.
int source[4] = {4, 2, 7, 1};
int destination[4];
int n = 4;

for (int i = 0; i < n; i++) {
    destination[i] = source[i];
}

The condition i < n makes the loop run for i = 0, 1, 2, and 3. After i = 3 is copied, the update makes i equal to 4, and the condition fails. Neither array is accessed at index 4, because index 4 is outside the bounds of an array whose length is 4.

CHECKPOINT 1Not answered

After the iteration with i = 1 completes, what is destination?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A second variable does not always mean a second array

An element-by-element copy creates separate storage and then fills it with the source values. Java assignment behaves differently: destination = source makes both variables refer to the same array. The statement copies an array reference, not its four values into new cells.

JAVAIn Java, allocating destination first and copying each value creates an independent array.
int[] source = {4, 2, 7, 1};
int[] destination = new int[4];

for (int i = 0; i < source.length; i++) {
    destination[i] = source[i];
}

Built-in C++ arrays cannot be duplicated with direct array assignment. In Java, destination = source is valid syntax, but it creates an alias instead of an independent duplicate. The explicit copy loop gives both languages the required result: destination[i] receives one value from source[i] inside storage that belongs to destination.

CHECKPOINT 2Not answered

Replace the Java aliasing statement with the element-copying assignment used inside the loop.

destination = source

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Changing the source exposes whether the duplicate is independent

After the copy, the arrays both contain [4, 2, 7, 1], but they still occupy different storage. Now change source[1] from 2 to 9. A correct duplicate stays [4, 2, 7, 1], because its index 1 contains its own copied value. The source becomes [4, 9, 7, 1].

TEXTSeparate storage keeps the destination unchanged when source[1] changes.
Before the change:
source      = [4, 2, 7, 1]
destination = [4, 2, 7, 1]

source[1] = 9

After the change:
source      = [4, 9, 7, 1]
destination = [4, 2, 7, 1]

If destination was made an alias of source, changing source[1] would also make destination appear to contain 9 at index 1. That result does not prove a copy happened. An independent duplicate is tested by changing one array and checking that the other keeps its original value at the same index.

Previous · Print Alternate Elements of an ArrayNext part · Quiz