Q44: What are the Time Complexities (Big O) of common Array operations?

  • push() / pop(): $O(1)$ (end of array).
  • unshift() / shift(): $O(n)$ (requires re-indexing elements).
  • indexOf() / includes(): $O(n)$ linear search.
  • sort(): $O(n \log n)$ average time complexity.
Key Takeaways
  • Adding/removing from start (shift/unshift) is $O(n)$.
  • Array access by numeric index (arr[i]) is $O(1)$.
Knowledge Check

Why is array.unshift() an O(n) operation whereas array.push() is O(1)?