DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Duplicate Zeros - I

const duplicateZeros = function (arr) {
    const newArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == 0) {
      newArray.push(0);
      newArray.push(0);
    } else {
      newArray.push(arr[i]);
    }      
  } 
    for (let i = 0; i < arr.length; i++) {
        arr[i] = newArray[i]
    }
  return newArray;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)