Skip to content Skip to sidebar Skip to footer

Time Complexity Of Array.from

What would be the time complexity of Array.from(). For example: const set = new Set(); set.add('car'); set.add('cat'); set.add('dog'); console.log(Array.from(set)); // time comple

Solution 1:

It's O(n). When used on an iterable (like a Set), Array.from iterates over the iterable and puts every item returned into the new array, so there's an operation for every item returned by the iterable.

Solution 2:

It is always going to be O(n) as the number of iterations would be directly proportional to the number of elements in the set. Actual Time complexity would be O(n) for retrieving values from a set O(n) for pushing into an array.

O(n) + O(n) = O(2n)

But since we do not consider a constant value when calculating using n value it should be O(n).

Post a Comment for "Time Complexity Of Array.from"