# How to solve this Array Duplication case t

Implement a function “duplicate” to duplicate an array, as shown below:

For input arr=[1,2] duplicate(arr) should return [1,2,1,2]

``````//Write your function here

function duplicate(arr) {
let array1 = [1, 2];
let array2 = [1, 2];
return arr = array1.concat(array2);
}
console.log(duplicate());

// Input and output has already been handled for you

process.stdin.resume();
process.stdin.setEncoding('utf8');

let remainder = '';
process.stdin.on('data', function (chunk) {
let arr = chunk.toString()
arr = arr.split(' ')
let nums = []
for(const c of arr) {
nums.push(parseInt(c))
}

const ans = duplicate(nums).join(' ')
process.stdout.write(ans)
process.exit();
});
``````

Hello there.

Do you have a question?

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

``````//Write your function here

function duplicate(arr) {
}

// Input and output has already been handled for you

process.stdin.resume();
process.stdin.setEncoding('utf8');

let remainder = '';
process.stdin.on('data', function (chunk) {
let arr = chunk.toString()
arr = arr.split(' ')
let nums = []
for(const c of arr) {
nums.push(parseInt(c))
}

const ans = duplicate(nums).join(' ')
process.stdout.write(ans)
process.exit();
});
``````

So, this was the original question, which I tried to solve and tried to implement a function “duplicate” to duplicate an array, as shown below:
For input arr = [1, 2] duplicate (arr) should return [1,2,1,2].

I was able to get the answer as displayed above, but, I had to change the function calling method, this makes the solution incorrect.

BASICALLY, they are saying:

``````Your Output

[ 1, 2, 1, 2 ] 1 2 1 2
Incorrect Output

Expected Output

NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
``````

What to do, I can’t understand whats going on.

Who is “they”? Where did this problem come from? What values are they passing to your function?

1 Like

You never use the function argument `arr`, and you hardcode these arrays:

Those are red flags when writing a function like this.

What changes did you make? What does your function look like now?

I am trying to solve a problem, the problem is

Implement a function “duplicate” to duplicate an array, as shown below:

For input arr=[1,2] duplicate(arr) should return [1,2,1,2]

The code given for me to try making a function is

``````//Write your function here

function duplicate(arr) {
// Write Logic here
}

``````

Here, “THEY” imply to the question. The question is asking me to make a function that basically clones the array. I am not able to understand how to do that.
But how to make it into the function??

``````
function duplicate(arr) {
let array1 = [1, 2];
let array2 = [1, 2];
return arr = array1.concat(array2);
}
console.log(duplicate());
``````

This is how my function looks like

I don’t know how to do it.
Should I write array1 and array2 outside the ‘funciton’ ???

You should not write `array1` or `array2` at all. You should only use `arr`.

It seems like you are missing some fundamental knowledge.

All the stuff in the ()s for a function are the arguments. Those are the inputs. You need to use the arguments to make the result that the array is supposed to output.

So they will call `duplicate([1, 2])` or `duplicate([42, 13, 99])`, and your function should return the correct result based upon that `arr` was given to the function.

I am not understanding the “Expected Output” shown above. One, it is not an array and two, I am not sure what the input array is that is supposed to produce this.

Based on the instructions, I would expect your function to take an array (in your case you have accomplished that by having `arr` as the function parameter). Next, I would expect your function to iterate through `arr` to figure out what to “duplicate” and add to an array that will be returned by the function.

As @JeremyLT said above, your function should be able to handle any array (not just `[1, 2]` as a value passed to it).

@RandellDawson @JeremyLT
Ohh okay.

But, if I had to use “duplicate(arr)” to call the function which basically duplicates an array: arr = [1,2] as [1,2,1,2].

How do I do that?

Let me try:

``````function duplicate(arr) {
let result = [1,2]
return arr = result.splice(1, 0, 1,2);
}
duplicate(arr);
``````

or should I do it like this:

``````function duplicate(arr) {
let result = [1,2]
return arr = result.splice(1, 0, result);
}
duplicate(arr);
``````

What can I do. I can’t understand.

You are attempting to hard code the values. You can not use the `[1,2]` inside the function.

Can you explain to us what you think the `arr` represents in the following function declaration? What is the purpose of `arr`?

``````function duplicate(arr) {

}``````

I would say arr is the parameters.

Okay, I got it.

I am sorry for taking your time, but, can you please tell me, how will I make a function which ultimately duplicates that array??

If you are not able to answer the above question, I might suggest you check out this lesson from freeCodeCamp’s Basic JavaScript course. It does a good job of explaining the basics of values passed to a function and doing something with the value.

We will not directly tell you how to code this problem. We will attempt to guide you to a solution, but you will need to learn it for yourself and attempt to apply what you learn to solve the problem.

If I call the below function like:

``````function duplicate(arr) {

}
duplicate([1, 2, 3, 4, 5));
``````

What is the value of `arr`?

1 Like

I would say, the value of arr here is [1, 2, 3, 4, 5], which is passed by you as argument. But, the code above should not work as there is nothing inside that function.

And thank you so much for your valuable guidance sir.

You are correct about the above example not doing anything. That was just an example to make sure you understood what `arr` meant.

Based on the instructions, I would expect your function to take an array (in your case you have accomplished that by having arr as the function parameter). Next, I would expect your function to iterate through arr to figure out what to “duplicate” and add to an array that will be returned by the function.

Expanding on what I said before, do you understand how to iterate through an array and access each value of the array? If so, then since you already seem to understand how to combine two arrays using `concat`, then you just need to create a new array by iterating through the original array and put each value of it into the new array. After that, you would return the combined result of the original array and the new array of duplicates.

So it as long as you understand how to iterate through an array and access each value, the only other thing you need to know how to do is how to put each accessed value into a new array. To do that, you would first need to know how to create an empty array.

See if you can put all this together and if you get stuck, post the latest version of the code you are try to use.

``````function duplicate(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
arr.push(arr[i]);
}
return arr;
}
``````

Is this good?? It solved the problem.

That method works also. There is always more than one way to solve most problems.

The only issue I see with your solution, is that you actually mutate/change the original array by pushing the duplicates to it. It is considered a better practice to not do that.

Using your current logic along with what I originally mentioned in an earlier reply, you could do the following to accomplish that:

``````function duplicate(arr) {
const dupArr = [];
for (let i = 0; i < arr.length; i++) {
dupArr.push(arr[i]);
}
return arr.concat(dupArr);
}
``````

Actually, a simpler solution is to take advantage of the fact that `concat` returns a new array, so you can simply write:

``````function duplicate(arr) {
return arr.concat(arr);
}
``````

I could have guided you to the second solution above, but I wanted you to write more code so you could see more of what is going on instead of just using a built-in function to accomplish the task.

1 Like