5
\$\begingroup\$

I want to achieve this

var arr = [1,2,3];
var duplicateArr = duplicate(arr, 2) // will be [1,2,3,1,2,3]
var duplicateArr2 = duplicate(arr, 3) // will be [1,2,3,1,2,3,1,2,3]

I have implemented it like this:

function duplicate(arr, n) {
 var len = arr.length;
 if (n == 0 || n == 1) {
 return arr;
 }
 if (n > 1) {
 for (var j = 0; j < n - 1; j++) {
 for (var i = 0; i < len; i++) {
 arr.push(arr[i])
 }
 }
 }
 console.log(arr);
}
duplicate([1, 2, 3], 3);

I know this may not be the efficient way to do it.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 29, 2016 at 18:50
\$\endgroup\$
1
  • \$\begingroup\$ One small remark: You should consider renaming your function. The word "duplicate" is usually used for creating a single copy of something. In your case "repeat" would be more appropriate. Also a hint what is repeated would be sensible, so I'd call it repeatArray. \$\endgroup\$ Commented Oct 4, 2016 at 11:22

3 Answers 3

6
\$\begingroup\$

The special treatment for n == 0 and n == 1, and the n > 1 condition are unnecessary. The outer loop condition already handles these cases, so this is roughly equivalent:

function duplicate(arr, n) {
 var len = arr.length;
 for (var j = 1; j < n; j++) {
 for (var i = 0; i < len; i++) {
 arr.push(arr[i]);
 }
 }
 return arr;
}

I say "roughly", because I dropped the console.log.

answered Sep 29, 2016 at 20:18
\$\endgroup\$
0
7
\$\begingroup\$

You can avoid the nested for loops by adding the entire array at the same time. (I'm also using Janos' suggestion here)

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

We need the original array unmodified, so we can keep adding it, so I'm using a new result array. This has a few benefits. Firstly it's more common to leave the input array unmodified and return a new array, and secondly it returns an empty array when n=0, which I think makes more sense.

This uses the ES6 spread syntax. If you don't want to use ES6 you can replace

result.push(...arr);

with

result = result.concat(arr);
answered Oct 4, 2016 at 10:13
\$\endgroup\$
-2
\$\begingroup\$

I think, this will reduce one loop

function duplicate(arr, n) {
 var len = arr.length;
 if (n == 0 || n == 1) {
 return arr;
 }
 if (n > 1) {
 for (var j = 0; j < n - 1; j++) {
 arr = arr.concat(arr); //**changed here**
 }
 }
 console.log(arr);;
}
answered Sep 30, 2016 at 5:55
\$\endgroup\$
2
  • 1
    \$\begingroup\$ We generally like to see more explanation in our reviews. How does this work? Also, have you tested it? Instead of concatenating the original array, it doubles the array. It will grow exponentially rather than linearly. \$\endgroup\$ Commented Sep 30, 2016 at 8:45
  • 1
    \$\begingroup\$ Doing concat many times is much slower because it makes progressively growing copies of the intermediate array. In case of large n this code will eat lots of memory too. And your code is simply wrong, it doubles the array in each iteration thus instead of n copies, there will be 2^n \$\endgroup\$ Commented Sep 30, 2016 at 9:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.