Inserting Elements in an Array - Array Operations
Last Updated :
07 Nov, 2024
In this post, we will look into insertion operation in an Array, i.e., how to insert into an Array, such as:
- Insert Element at the Beginning of an Array
- Insert Element at a given position in an Array
- Insert Element at the End of an Array
Insert Element at the Beginning of an Array
Inserting an element at the beginning of an array involves shifting all existing elements one position to the right to create an empty space for the new element at index 0.
Examples:
Input: arr[] = [10, 20, 30, 40], ele = 50
Output: [50, 10, 20, 30, 40]
Input: arr[] = [], ele = 20
Output: [20]
To know more about the implementation, please refer Insert Element at the Beginning of an Array.
Insert Element at a given position in an Array
Inserting an element at a given position in an array involves shifting the elements from the specified position onward one index to the right to make an empty space for the new element. This operation ensures that the order of the existing elements is preserved while placing the new element at the desired index. After shifting the elements, the new element is inserted at the target position.
Examples:
Input: arr[] = [10, 20, 30, 40], pos = 2, ele = 50
Output: [10, 50, 20, 30, 40]
Input: arr[] = [], pos = 1, ele = 20
Output: [20]
Input: arr[] = [10, 20, 30, 40], pos = 5, ele = 50
Output: [10, 20, 30, 40, 50]
To know more about the implementation, please refer Insert Element at a Given Position in an Array.
Insert Element at the End of an Array
Inserting an element at the end of an array involves adding the new element to the last available index of the array. Inserting an element at the end of the array takes constant time provided there is enough space in the array to add the new element.
Examples:
Input: arr[] = [10, 20, 30, 40], ele = 50
Output: [10, 20, 30, 40, 50]
Input: arr[] = [], ele = 20
Output: [20]
To know more about the implementation, please refer Insert Element at the End of an Array.
Related Posts:
Similar Reads
Deleting Elements in an Array - Array Operations In this post, we will look into deletion operation in an Array, i.e., how to delete an element from an Array, such as:Delete an Element from the Beginning of an ArrayDelete an Element from a Given Position in an ArrayDelete First Occurrence of Given Element from an ArrayRemove All Occurrences of an
4 min read
Search, Insert, and Delete in an Sorted Array | Array Operations How to Search in a Sorted Array?In a sorted array, the search operation can be performed by using binary search.Below is the implementation of the above approach:C++// C++ program to implement binary search in sorted array #include <bits/stdc++.h> using namespace std; int binarySearch(int arr[
15+ min read
Insert Element at a Given Position in an Array Given an array of integers, the task is to insert an element at a given position in the array.Examples:Input: arr[] = [10, 20, 30, 40], pos = 2, ele = 50Output: [10, 50, 20, 30, 40]Input: arr[] = [], pos = 1, ele = 20Output: [20]Input: arr[] = [10, 20, 30, 40], pos = 5, ele = 50Output: [10, 20, 30,
7 min read
Search, Insert, and Delete in an Unsorted Array | Array Operations In this post, a program to search, insert, and delete operations in an unsorted array is discussed.Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:C++// C++ prog
15+ min read
Insert Element at the End of an Array Given an array of integers, the task is to insert an element at the end of the array.Examples:Input: arr[] = [10, 20, 30, 40], ele = 50Output: [10, 20, 30, 40, 50]Input: arr[] = [], ele = 20Output: [20]Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approach 1] Us
5 min read