Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the complexity of adding\removing objects in NSMutableArray? [closed]

I think that to add an object we need: create new array with a bigger size + make a copy of older array + add an element. So final complexity is O(N), where N - final number of elements.

Removing - O(N) also.

Am I wrong?

Thx.

like image 954
AndrewShmig Avatar asked Nov 24 '25 17:11

AndrewShmig


1 Answers

I think that to add an object we need: create new array with a bigger size + make a copy of older array + add an element.

NOOOoooo....

To add an object, no new arrays are created and done all those stuffs.

If you remember cocoa has all pointers. And if you see C/C++ with pointers, just take it as a linked list. To add a new element only its address is saved in the list, and head/tail is adjusted if required.

Same case is here with MutableArrays.

Complexity should be O(1).

like image 59
Anoop Vaidya Avatar answered Nov 27 '25 10:11

Anoop Vaidya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!