as we know, the time complexity of deleting an array element is o(n), so what is the time complexity of deleting the entire array?

i think is o(1), because the array address space is continuous. is my guess correct?

1 Answers

0
faissaloo On

Deleting the array itself would be O(1) because it would simply release the memory to the pool while deleting every item in the array would be O(n) because each item needs to be removed individually.

There are exceptions, some implementations may for example use an algorithm that clears memory in chunks larger than the size of each item in the array (which would be O(n/c) where c is how many times bigger a chunk is compared to an item in the array), and some languages might simply release the original array and have pointers point to a new empty array which would be O(1). The above answer however is assuming a naive implementation.