What is the time complexity of the unshift() method in JavaScript arrays?

  • O(n)
  • O(1)
  • O(log n)
  • O(n log n)
The unshift() method in JavaScript arrays has a time complexity of O(n), where "n" represents the number of elements in the array. This is because it needs to shift all existing elements to make room for the new element at the beginning. The higher the number of elements, the longer it takes.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *