Algorithms and Data Structures · Junior · Question 68 of 75

What is the complexity of arr.slice() (copying the whole array)?

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²).

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 68

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz