Insert Element in Array

Inserting an element into an array is a common operation in programming.

🛠️ Ways to Insert an Element into an Array

Inserting an element into an array depends on the position where you want to insert it. Here are some common methods:

  1. Insert at the Beginning
  2. Insert at the End
  3. Insert at a Specific Position

1️⃣ Insert at the Beginning

If we insert an element at start of the array, we need to consider few things:

  1. Is array empty
  2. If array is not empty and have space at the end
  3. If array is full

Insert at start when array is empty

When array is empty, we can simply insert the element at index 0.

Insert at start when array is empty in Python

To insert an element at the start of an empty array in Python, you can use the insert method by passing index 0 as first argument and element to insert as second argument.

arr = []
element = 10 # element to insert
# Insert at start
arr.insert(0, element)
# Print array
print(arr)

Complexity Analysis for Insertion at Start when Array is Empty

  • Time Complexity: O(1) - as we are directly inserting at index 0.
  • Space Complexity: O(1) - as we are not using any extra space for insertion.

Insert at start when array is not empty and have space at the end

When array is not empty and have space at the end, we need to shift all elements one position to the right and then insert the new element at index 0.

Insert at start when array is not empty and have space at the end in Python

To insert an element at the start of a non-empty array in Python, you can use the insert method by passing index 0 as first argument and element to insert as second argument.

arr = [20, 30, 40] # existing elements
element = 10 # element to insert
# Insert at start
arr.insert(0, element)
# Print array
print(arr)

Note that in Python, lists are dynamic arrays, so you don’t need to worry about shifting elements manually.

Complexity Analysis for Insertion at Start

  • Time Complexity: O(n) - because we may need to shift all elements to the right.
  • Space Complexity: O(1) - as we are not using any extra space for insertion.

Insert at start when array is full

When array is full, we need to create a new array with larger size. Then we insert the new element at index 0 and copy all elements from old array to new array starting from index 1. Finally, we update the reference of old array to new array.

Insert at start when array is full in Python

To insert an element at the start of a full array in Python, you can use the insert method by passing index 0 as first argument and element to insert as second argument. Since Python lists are dynamic, you don’t need to create a new array manually.

arr = [20, 30, 40] # existing elements
element = 10 # element to insert
# Insert at start
arr.insert(0, element)
# Print array
print(arr)

Complexity Analysis for Insertion at Start when Array is Full

  • Time Complexity: O(n) - because we need to copy all elements to the new array
  • Space Complexity: O(n) - as we are creating a new array with larger size.

Share & Connect