Array Cheatsheet
Arrays hold values of the same type at contiguous memory locations. In an array, we're usually concerned about two things - the position/index of an element and the element itself.
Operation | Big-O | Note |
---|---|---|
Access | O(1) | |
Search | O(n) | |
Search (sorted array) | O(log(n)) | |
Insert | O(n) | Insertion would require shifting all the subsequent elements to the right by one and that takes O(n) |
Insert (at the end) | O(1) | Special case of insertion where no other element needs to be shifted |
Remove | O(n) | Removal would require shifting all the subsequent elements to the left by one and that takes O(n) |
Remove (at the end) | O(1) | Special case of removal where no other element needs to be |