Array Manipulation Techniques
1. Insertion
Inserting an element into an array can be done at the beginning, in the middle, or
at the end of the array. Depending on the position and the array type (static or
dynamic), the approach can vary.
At the End (Efficient):
For dynamic arrays, inserting at the end is typically O(1), as the array is just
extended with the new element.
Example in Python:
arr.append(10) # Insert 10 at the end of the array
Example in JavaScript:
arr.push(10); // Insert 10 at the end of the array
At the Beginning (Inefficient):
Inserting at the beginning of an array requires shifting all the elements to the
right, making it O(n).
Example in Python:
arr.insert(0, 10) # Insert 10 at the beginning of the array
Example in JavaScript:
arr.unshift(10); // Insert 10 at the beginning of the array
At the Middle (Inefficient):
Insertion at the middle also requires shifting elements, making it O(n). You need
to find the correct position and then shift elements around.
, Example in Python:
arr.insert(2, 10) # Insert 10 at index 2 (middle)
Example in JavaScript:
arr.splice(2, 0, 10); // Insert 10 at index 2 (middle)
2. Deletion
Deleting an element from an array can also be done at the beginning, end, or
middle, with varying time complexities depending on the position.
From the End (Efficient):
Deleting an element from the end is typically O(1), as no elements need to be
shifted.
Example in Python:
arr.pop() # Remove the last element
Example in JavaScript:
arr.pop(); // Remove the last element
From the Beginning (Inefficient):
Deleting from the beginning requires shifting all the elements to the left, making
it O(n).
Example in Python:
arr.pop(0) # Remove the first element
Example in JavaScript:
arr.shift(); // Remove the first element
1. Insertion
Inserting an element into an array can be done at the beginning, in the middle, or
at the end of the array. Depending on the position and the array type (static or
dynamic), the approach can vary.
At the End (Efficient):
For dynamic arrays, inserting at the end is typically O(1), as the array is just
extended with the new element.
Example in Python:
arr.append(10) # Insert 10 at the end of the array
Example in JavaScript:
arr.push(10); // Insert 10 at the end of the array
At the Beginning (Inefficient):
Inserting at the beginning of an array requires shifting all the elements to the
right, making it O(n).
Example in Python:
arr.insert(0, 10) # Insert 10 at the beginning of the array
Example in JavaScript:
arr.unshift(10); // Insert 10 at the beginning of the array
At the Middle (Inefficient):
Insertion at the middle also requires shifting elements, making it O(n). You need
to find the correct position and then shift elements around.
, Example in Python:
arr.insert(2, 10) # Insert 10 at index 2 (middle)
Example in JavaScript:
arr.splice(2, 0, 10); // Insert 10 at index 2 (middle)
2. Deletion
Deleting an element from an array can also be done at the beginning, end, or
middle, with varying time complexities depending on the position.
From the End (Efficient):
Deleting an element from the end is typically O(1), as no elements need to be
shifted.
Example in Python:
arr.pop() # Remove the last element
Example in JavaScript:
arr.pop(); // Remove the last element
From the Beginning (Inefficient):
Deleting from the beginning requires shifting all the elements to the left, making
it O(n).
Example in Python:
arr.pop(0) # Remove the first element
Example in JavaScript:
arr.shift(); // Remove the first element