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:
Insert at the Beginning
Insert at the End
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:
Is array empty
If array is not empty and have space at the end
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 C
To insert an element at the start of an empty array in C, you can directly assign the value to the first index of the array.
#include<stdio.h>
#defineMAX100
intmain() {
int arr[MAX];
int n =0; // current size of array
int element =10; // element to insert
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
printf("%d", arr[i]);
}
return0;
}
Insert at start when array is empty in C++
To insert an element at the start of an empty array in C++, you can directly assign the value to the first index of the array.
#include<iostream>
#defineMAX100
usingnamespace std;
intmain() {
int arr[MAX];
int n =0; // current size of array
int element =10; // element to insert
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
cout << arr[i] <<"";
}
return0;
}
Insert at start when array is empty in Java
To insert an element at the start of an empty array in Java, you can directly assign the value to the first index of the array.
publicclassMain {
publicstaticvoidmain(String[] args) {
int[] arr =newint[100];
int n =0; // current size of array
int element =10; // element to insert
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
System.out.print(arr[i] +"");
}
}
}
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)
Insert at start when array is empty in JavaScript
To insert an element at the start of an empty array in JavaScript, you can use the unshift method. unshift adds one or more elements to the beginning of an array.
let arr = [];
let element =10; // element to insert
// Insert at start
arr.unshift(element);
// Print array
console.log(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 C
To insert an element at the start of a non-empty array in C, you need to shift all elements to the right and then insert the new element at index 0.
#include<stdio.h>
#defineMAX100
intmain() {
int arr[MAX] = {20, 30, 40}; // existing elements
int n =3; // current size of array
int element =10; // element to insert
// Shift elements to the right
for(int i = n; i >0; i--) {
arr[i] = arr[i -1];
}
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
printf("%d", arr[i]);
}
return0;
}
Insert at start when array is not empty and have space at the end in C++
To insert an element at the start of a non-empty array in C++, you need to shift all elements to the right and then insert the new element at index 0.
#include<iostream>
#defineMAX100
usingnamespace std;
intmain() {
int arr[MAX] = {20, 30, 40}; // existing elements
int n =3; // current size of array
int element =10; // element to insert
// Shift elements to the right
for(int i = n; i >0; i--) {
arr[i] = arr[i -1];
}
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
cout << arr[i] <<"";
}
return0;
}
Insert at start when array is not empty and have space at the end in Java
To insert an element at the start of a non-empty array in Java, you need to shift all elements to the right and then insert the new element at index 0.
publicclassMain {
publicstaticvoidmain(String[] args) {
int[] arr =newint[100];
arr[0] =20;
arr[1] =30;
arr[2] =40; // existing elements
int n =3; // current size of array
int element =10; // element to insert
// Shift elements to the right
for(int i = n; i >0; i--) {
arr[i] = arr[i -1];
}
// Insert at start
arr[0] = element;
n++; // increase size
// Print array
for(int i =0; i < n; i++) {
System.out.print(arr[i] +"");
}
}
}
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.
Insert at start when array is not empty and have space at the end in JavaScript
To insert an element at the start of a non-empty array in JavaScript, you can use the unshift method. unshift adds one or more elements to the beginning of an array.
let arr = [20, 30, 40]; // existing elements
let element =10; // element to insert
// Insert at start
arr.unshift(element);
// Print array
console.log(arr);
Note that in JavaScript also, arrays are dynamic, 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 C
To insert an element at the start of a full array in C, you need to create a new array with larger size, insert the new element at index 0, and copy all elements from the old array to the new array starting from index 1.
#include<stdio.h>
#include<stdlib.h>
#defineMAX3
intmain() {
int* arr = (int*)malloc(MAX *sizeof(int));
arr[0] =20;
arr[1] =30;
arr[2] =40; // existing elements
int n = MAX; // current size of array
int element =10; // element to insert
// Create new array with larger size
int* newArr = (int*)malloc((n +1) *sizeof(int));
// Insert at start
newArr[0] = element;
// Copy old elements to new array
for(int i =0; i < n; i++) {
newArr[i +1] = arr[i];
}
n++; // increase size
// Free old array
free(arr);
arr = newArr; // update reference
// Print array
for(int i =0; i < n; i++) {
printf("%d", arr[i]);
}
// Free new array
free(arr);
return0;
}
Insert at start when array is full in C++
To insert an element at the start of a full array in C++, you need to create a new array with larger size, insert the new element at index 0, and copy all elements from the old array to the new array starting from index 1.
#include<iostream>
#defineMAX3
usingnamespace std;
intmain() {
int* arr =newint[MAX];
arr[0] =20;
arr[1] =30;
arr[2] =40; // existing elements
int n = MAX; // current size of array
int element =10; // element to insert
// Create new array with larger size
int* newArr =newint[n +1];
// Insert at start
newArr[0] = element;
// Copy old elements to new array
for(int i =0; i < n; i++) {
newArr[i +1] = arr[i];
}
n++; // increase size
// Free old array
delete[] arr;
arr = newArr; // update reference
// Print array
for(int i =0; i < n; i++) {
cout << arr[i] <<"";
}
// Free new array
delete[] arr;
return0;
}
Insert at start when array is full in Java
To insert an element at the start of a full array in Java, you need to create a
new array with larger size, insert the new element at index 0, and copy all elements from the old array to the new array starting from index 1.
publicclassMain {
publicstaticvoidmain(String[] args) {
int[] arr =newint[3];
arr[0] =20;
arr[1] =30;
arr[2] =40; // existing elements
int n =3; // current size of array
int element =10; // element to insert
// Create new array with larger size
int[] newArr =newint[n +1];
// Insert at start
newArr[0] = element;
// Copy old elements to new array
for(int i =0; i < n; i++) {
newArr[i +1] = arr[i];
}
n++; // increase size
arr = newArr; // update reference
// Print array
for(int i =0; i < n; i++) {
System.out.print(arr[i] +"");
}
}
}
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)
Insert at start when array is full in JavaScript
To insert an element at the start of a full array in JavaScript, you can use the unshift method. Since JavaScript arrays are dynamic, you don’t need to create a new array manually.
let arr = [20, 30, 40]; // existing elements
let element =10; // element to insert
// Insert at start
arr.unshift(element);
// Print array
console.log(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.