What is the time complexity of new Array(n).fill('apple') in JavaScript?
I was searching for the answer to this question but was not able to find any.
What is the time complexity of new Array(n).fill('apple')
?
For n=5
, this will create an array with 5 'apple' strings: ['apple', 'apple', 'apple', 'apple', 'apple']
My assumption is new Array(5) will first create an array with 5 empty slots and then iterate through it to put 'apple' in each slot. In this case, the time complexity is O(N), N is the length of the array?
However, I also hear that some say that since it's a built-in method, it will only take O(1).
from Recent Questions - Stack Overflow https://ift.tt/2WhNALb
https://ift.tt/eA8V8J
Comments
Post a Comment