DSA FUNDAMENTALS › ARRAY BASICS
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.
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].
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.
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.
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.
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.
Replace the Java aliasing statement with the element-copying assignment used inside the loop.
destination = sourceCheckpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
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].
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.