Insert and Delete in an Array
Imagine students sitting in a row of benches. A new student must sit in the 3rd seat. Everyone from seat 3 onward moves one seat right. When a student leaves, everyone after them moves one seat left to fill the gap.
Arrays work exactly like this. In this lesson you will write programs to insert and delete an element at any position. These are very common lab and exam questions.
What You Will Learn
- Why insert and delete need shifting in an array.
- How to insert an element at a given position.
- How to delete an element from a given position.
- How to delete an element by its value.
- The time cost of insertion and deletion.
Insertion and Deletion in an Array Explained
Exam definition: Insertion adds a new element at a given position by shifting the elements after it one place to the right. Deletion removes an element by shifting the elements after it one place to the left. The count n increases or decreases by 1.
Memory trick: Insert → shift right, start from the end (like making space in a queue from the back). Delete → shift left, start from the gap (like closing a gap in a line).
Algorithm (steps)
INSERT value at position pos (1-based):
1. If n == MAX, print "array full" and stop.
2. for i = n-1 down to pos-1: a[i+1] = a[i] (shift right)
3. a[pos-1] = value
4. n = n + 1
DELETE from position pos (1-based):
1. for i = pos-1 to n-2: a[i] = a[i+1] (shift left)
2. n = n - 1
Example 1: Insert an Element at a Given Position
#include <stdio.h>
#define MAX 100
int main(void)
{
int a[MAX] = {10, 20, 30, 40, 50};
int n = 5, value, pos, i;
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter position (1 to %d): ", n + 1);
scanf("%d", &pos);
if (n == MAX || pos < 1 || pos > n + 1)
{
printf("Insertion not possible\n");
return 1;
}
for (i = n - 1; i >= pos - 1; i--) /* shift right, from the end */
{
a[i + 1] = a[i];
}
a[pos - 1] = value;
n++;
printf("Array after insertion:");
for (i = 0; i < n; i++)
printf(" %d", a[i]);
printf("\n");
return 0;
}
Output
Enter value to insert: 25 Enter position (1 to 6): 3 Array after insertion: 10 20 25 30 40 50
Code Explanation
int a[MAX]has room for 100 elements, but onlyn = 5are used. The free space at the end lets us insert.- The
ifchecks: the array is not full, and the position is between 1 and n + 1 (n + 1 means "add at the end"). - Position 3 means index 2, so we use
pos - 1. Users count from 1; C counts from 0. - The loop moves elements one step right, starting from the last one. If we started from the front, we would overwrite values before moving them.
a[pos - 1] = value;puts 25 in the empty slot, andn++records that the array grew.
How the Shifting Works (Dry Run)
Start : 10 20 30 40 50 __
i = 4: a[5]=a[4]: 10 20 30 40 50 50
i = 3: a[4]=a[3]: 10 20 30 40 40 50
i = 2: a[3]=a[2]: 10 20 30 30 40 50
a[2] = 25 : 10 20 25 30 40 50 (n = 6)
Why from the end? If you did a[3] = a[2] first, 40 would be lost before it moved. Moving the last element first always copies into an empty or already-copied slot.
Example 2: Delete by Position and Delete by Value
#include <stdio.h>
void print(const int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf(" %d", a[i]);
printf("\n");
}
/* removes the element at 1-based position pos; returns the new size */
int delete_at(int a[], int n, int pos)
{
int i;
if (pos < 1 || pos > n)
return n; /* invalid position: nothing changes */
for (i = pos - 1; i < n - 1; i++) /* shift left, from the gap */
a[i] = a[i + 1];
return n - 1;
}
/* removes the first element equal to value; returns the new size */
int delete_value(int a[], int n, int value)
{
int i;
for (i = 0; i < n; i++)
{
if (a[i] == value)
return delete_at(a, n, i + 1);
}
printf("%d not found\n", value);
return n;
}
int main(void)
{
int a[] = {11, 22, 33, 44, 55, 66};
int n = 6;
printf("Original :");
print(a, n);
n = delete_at(a, n, 2);
printf("Delete pos 2 :");
print(a, n);
n = delete_value(a, n, 55);
printf("Delete 55 :");
print(a, n);
n = delete_value(a, n, 99);
return 0;
}
Output
Original : 11 22 33 44 55 66 Delete pos 2 : 11 33 44 55 66 Delete 55 : 11 33 44 66 99 not found
- Deletion shifts left starting at the gap and moving toward the end.
- The last old element stays in memory, but since
nis smaller, we never look at it again. - The functions return the new size, because they can change the array (shared address) but not the caller's
n.
More practice: program to delete elements from an array in C.
Time Complexity of Insertion and Deletion
| Operation | Best case | Worst case |
|---|---|---|
| Insert | At the end: O(1), no shifting | At the start: O(n), shift all |
| Delete | Last element: O(1) | First element: O(n) |
| Access a[i] | O(1) always | |
This is why arrays are great for reading but slow for frequent insert/delete in the middle. A linked list (Module 16, Data Structures) solves this.
Important Points to Remember
- Insert: shift right, loop from the end down to the position.
- Delete: shift left, loop from the position up to the end.
- Convert user position to index with pos − 1.
- Check for a full array and an invalid position first.
- Update n after every insert (+1) or delete (−1).
- Both take O(n) time in the worst case.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Shifting right from the front | Same value copied everywhere | Loop from n - 1 down |
Using pos as the index directly | Inserted one place too far | Use pos - 1 |
Forgetting n++ / n-- | Last element missing or repeated in output | Update the count |
| No check for a full array | Writes past the end (undefined behaviour) | Check n == MAX |
Delete loop to i < n | Reads a[n], out of range | Loop to i < n - 1 |
Insert and Delete in Array Practice Questions
- Why must the right shift during insertion start from the last element?
- How many elements move when you insert at position 1 in an array of 8 elements?
- After deleting position 3 from
{5, 10, 15, 20, 25}, what is the array? - What is the worst-case time complexity of deletion in an array?
- Why does a delete function return the new size?
Show answers
- Starting from the front would overwrite the next element before it is moved.
- All 8 elements move.
{5, 10, 20, 25}.- O(n), when deleting the first element.
- The function can change the array's contents, but
nwas passed by value, so the caller needs the new size back.
Coding Exercise
Write a program that inserts 35 into the sorted array {10, 20, 30, 40, 50} at its correct place, so the array stays sorted. (Hint: shift right every element greater than 35.)
Show solution
#include <stdio.h>
int main(void)
{
int a[10] = {10, 20, 30, 40, 50};
int n = 5, value = 35, i;
i = n - 1;
while (i >= 0 && a[i] > value)
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = value;
n++;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
10 20 30 35 40 50
Quick Quiz: Insert and Delete in Array (MCQ)
-
During insertion, elements are shifted:
A) Left B) Right C) Not shifted D) Randomly
-
Worst-case time to insert into an array of n elements:
A) O(1) B) O(log n) C) O(n) D) O(n²)
-
To insert at user position 4, the value goes into index:
A) 4 B) 3 C) 5 D) 0
-
Deleting position 1 from
{7, 8, 9}gives:A)
{7, 8}B){8, 9}C){7, 9}D){9} -
Fastest place to insert in an array (with free space):
A) Beginning B) Middle C) End D) All are equal
Show answers
- B: right.
- C: O(n).
- B: index 3.
- B:
{8, 9}. - C: end, no shifting needed.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Insert | Shift right, loop from the end |
|---|---|
| Delete | Shift left, loop from the position |
| Position to index | pos − 1 |
| Before inserting | Check array full and valid position |
| Worst case | O(n) for both |
| Best case | O(1) at the end |
Frequently asked questions
- How do you insert an element in an array at a given position?
- Shift elements from the end one place right up to the position, place the value, then do n++.
- How do you delete an element from an array?
- Shift every element after it one place left, then do n−−.
- Why are insert and delete slow in arrays?
- Elements are contiguous, so making or closing a gap needs shifting up to n elements.
Remember
- Insert = new student, everyone moves right (start from the back).
- Delete = student leaves, everyone moves left to close the gap.
Next Topic
Next lesson: Linear and Binary Search in C. You will find an element in an array with linear search and the much faster binary search.