Algorithms and Data Structures · Junior · Question 68 of 75
What is the complexity of arr.slice() (copying the whole array)?
- 1O(log n)
- ✓Correct answer: O(n)
- 3O(1)
Explanation
A copy has to touch every element, so it's linear in time and memory. The same applies to [...arr], Array.from(arr) and structuredClone. Copying inside a loop easily turns an algorithm into O(n²).