Create Duplicate an Array
When working with arrays, there are situations where we may need to perform operations or modifications on an array while preserving the original data. In such cases, it's common to create a copy of the original array. By doing this, any modifications or operations can be performed on the duplicate array, ensuring that the original array remains unchanged. This approach allows us to retain the original array in its unaltered state while working on the duplicate.
Write a program that takes an array as input and creates a duplicate of the given array.

Example:
Input: nums = [4, 8, 15, 16, 23, 42]
Output: [4, 8, 15, 16, 23, 42]
Explanation: The duplicate array is identical to the input array, preserving the original array for future use.
Input: nums = [10, 20, 30, 40, 50]
Output: [10, 20, 30, 40, 50]
Approach
To create a duplicate array of nums called d_nums, we first need to declare d_nums with the same size as nums. After the declaration, we will assign values to each element in d_nums such that they match the corresponding elements in nums. This will be done by iterating over the array nums, and for every index i from 0 to n-1 (where n is the size of nums), we will set d_nums[i] = nums[i]. This process ensures that each element in d_nums is an exact copy of the element at the same index in nums.


Dry Run
Input: nums = [10, 20, 30, 40, 50], size = 5
Output: [10, 20, 30, 40, 50]
- A new array d_nums is declared with the same size as nums.
Initially, d_nums = [ , , , , ] (empty array of size 5). - Start copying elements from nums to d_nums:
- Iteration 1 (index = 0): d_nums[0] = nums[0]
d_nums = [10, , , , ] - Iteration 2 (index = 1): d_nums[1] = nums[1]
d_nums = [10, 20, , , ] - Iteration 3 (index = 2): d_nums[2] = nums[2]
d_nums = [10, 20, 30, , ] - Iteration 4 (index = 3): d_nums[3] = nums[3]
d_nums = [10, 20, 30, 40, ] - Iteration 5 (index = 4): d_nums[4] = nums[4]
d_nums = [10, 20, 30, 40, 50]
- Iteration 1 (index = 0): d_nums[0] = nums[0]
Final Output : The duplicate array is printed exactly as the input array: [10, 20, 30, 40, 50]
Code for All Languages
C++
#include <iostream>
using namespace std;
// Function to create a duplicate of the array and print it
void createAndPrintDuplicate(int nums[], int size) {
// Declare a new array d_nums with the same size as nums
int d_nums[size];
// Copy each element from nums to d_nums
for (int index = 0; index < size; ++index) {
d_nums[index] = nums[index];
}
// Print the elements of the duplicate array d_nums
for (int index = 0; index < size; ++index) {
cout << d_nums[index] << " ";
}
}
Java
import java.util.Scanner;
public class LearnYard {
// Function to create a duplicate of the array and print it
static void createAndPrintDuplicate(int[] nums, int size) {
// Declare a new array d_nums with the same size as nums
int[] d_nums = new int[size];
// Copy each element from nums to d_nums
for (int index = 0; index < size; index++) {
d_nums[index] = nums[index];
}
// Print the elements of the duplicate array d_nums
for (int index = 0; index < size; index++) {
System.out.print(d_nums[index] + " ");
}
}
}
Python
# Function to create a duplicate of the array and print it
def createAndPrintDuplicate(nums, size):
# Declare a new list d_nums with the same size as nums
d_nums = [0] * size
# Copy each element from nums to d_nums
for index in range(size):
d_nums[index] = nums[index]
# Print the elements of the duplicate array d_nums
print(*d_nums)
Javascript
// Function to create a duplicate of the array and print it
function createAndPrintDuplicate(nums, size) {
// Declare a new array d_nums with the same size as nums
let d_nums = new Array(size);
// Copy each element from nums to d_nums
for (let index = 0; index < size; index++) {
d_nums[index] = nums[index];
}
// Print the elements of the duplicate array d_nums
console.log(d_nums.join(" "));
}
Time Complexity : O(n)
The time complexity of the approach is determined by the iteration over the array nums. We iterate through each element of nums exactly once to copy it into d_nums. If n is the size of the array nums, the loop runs n times, and each assignment operation (d_nums[i] = nums[i]) takes constant time, O(1). Therefore, the total time complexity is O(n)
Space Complexity : O(n)
- Total Space Complexity: O(n)
- Input Array Space: The input array nums takes up space proportional to its size. If the array has n elements, the space required to store the array is O(n).
- Duplicate Array Space: The duplicate array d_nums also requires space proportional to its size, which is equal to the input array size. Thus, this adds an additional O(n) space.
- Variables: The variables size, index, and nums require constant space, O(1), because they do not grow with the size of the array.
- Overall, the total space complexity is O(n) + O(n) = O(n).
- Auxiliary Space Complexity: O(n)
- Auxiliary Space: This refers to the extra space used by the algorithm, excluding the input data. In this approach, the only auxiliary space used is for the duplicate array d_nums, which is O(n).
- Other temporary variables (like index) require constant space, O(1).