As this is something you should accomplish yourself, I will only provide the method to implement it, not the code:
If you would set the number at position index, you would overwrite the value that was there previously. So what you need to do is move every element one position towards the end of the array starting from index: num[x] becomes num[x+1], etc.
You will find out that you need to do this in reverse order, otherwise you will fill your array with the value in num[index].
During this process you will need to decide what to do with the last entry of the array (num[num.length - 1]):
- You could just overwrite it, discarding the value
- You could return it from your function
- You could throw an exception if it is non-zero
- You could create a new array that is 1 entry larger than the current array instead to keep all values
- etc.
After this, you have duplicated num[index]: the value is present in num[index+1], too, as you have moved it away.
Now it is possible to write the new value at the desired position without overriding an existing value.
EDIT
You have several errors in your code:
- You increment
k, you need to decrement it (k--, not k++)
- You modify
k again in your loop body: it is updated twice in each cycle
- If you start with
k = num.length, you will try to write at num[num.length + 1], which is not possible