# Program to insert, delete and search an element in an array

Operations of an array | Program to insert, delete and search an element in an array is discussed here. Given an array, the array operations like insert, delete and search an element in an array are performed and the result is displayed.

If the position where the element to be inserted is greater than the size of an array display “Invalid Input”.

Input format:

• Input consists of 3 integers and 1 array.
• Input the size of the array.
• Input the array elements.
• Input the position where the element should be inserted.
• Input the element to be inserted in that position.

Sample Input:

5  (size of the array)
1  (array elements)
2
3
4
5
4  (Position)
10 (Element to be inserted)

Sample  Output:

Array after insertion is:
1
2
3
10
4
5

## Algorithm to insert, delete and search an element in an array

##### Insertion
• Input the array elements, the position of the new element to be inserted and the new element.
• Insert the new element at that position and shift the rest of the elements to right by one position.
##### Deletion
• Input the array elements, the position of the new element to be inserted and the new element.
• Delete the element and shift the rest of the elements to left by one position.
##### Search
• Input the array elements, the element to be searched.
• Traverse the array and check if the element is present in the array.
• Display “Yes” if it is present in the array, else display “No”.

Program to insert, delete and search an element in an array is given below.

/* C program to insert an element in an array */
#include
int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int pos;
scanf(“%d”,&pos);
int ele;
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= pos – 1; i–)
arr[i+1] = arr[i];

arr[pos-1] = ele;

printf(“Array after insertion is:\n”);

for (i = 0; i <= n; i++)
printf(“%d\n”, arr[i]);
}

return 0;
}

#include<stdio.h>
#include<stdlib.h>
void insert(int n1, int *a, int l, int e)
{
int i;
printf(“Array after insertion is:\n”);
for(i=0;i<l-1;i++)
{
printf(“%d\n”,*(a+i));
}
printf(“%d\n”,e);
for(i=l-1;i<n1;i++)
{
printf(“%d\n”,*(a+i));
}
}

int main()
{
int *a,n1,i,l,e;
scanf(“%d”,&n1);
a=(int*)malloc(n1*sizeof(int));
for(i=0;i<n1;i++)
{
scanf(“%d”,a+i);
}
scanf(“%d”,&l);
if(l<=n1)
{
scanf(“%d”,&e);
insert(n1,a,l,e);
}
else
{
printf(“Invalid Input”);
}
return 0;
}

/* C++ program to insert an element in an array */
#include<iostream>
using namespace std;

int main()
{
int n;
cin >> n;
int arr[n];
int i;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
int pos;
cin >> pos;
int ele;
cin >> ele;
if(pos > n)
cout << “Invalid Input”;
else
{
for (i = n – 1; i >= pos – 1; i–)
arr[i+1] = arr[i];

arr[pos-1] = ele;

cout << “Array after insertion is:\n”;

for (i = 0; i <= n; i++)
cout << arr[i] << ” “;
}
return 0;
}

/* Java program to insert an element in an array */

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextInt();
int a[] = new int[n+1];
System.out.println(“Enter all the elements:”);
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print(“Enter the position where you want to insert element:”);
pos = s.nextInt();
System.out.print(“Enter the element you want to insert:”);
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i–)
{
a[i+1] = a[i];
}
a[pos-1] = x;
System.out.print(“After inserting:”);
for(int i = 0; i < n; i++)
{
System.out.print(a[i]+”,”);
}
System.out.print(a[n]);
}
}

# Python program to insert an element in an array

n = int(input())
sum = 0
arr = [1,2,3,4,5]

pos = int(input())
ele = int(input())
arr.insert(pos,ele)

for i in range(0,n):
print(arr[i], end = ” “)

## Program to delete an element in an array

/* C program to delete an element in an array */

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf(“Enter the number of elements of the array : “);
scanf(“%d”, &n);

printf(“\nInput the array elements : “);

for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);

printf(“\nEnter the position : “);
scanf(“%d”, &position);

if (position >= n+1)
printf(“\nDeletion not possible.\n”);
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c+1];

printf(“\nArray after deletion : “);

for (c = 0; c < n – 1; c++)
printf(“%d\n”, array[c]);
}

return 0;
}

#include<stdio.h>
#include<stdlib.h>
void delete(int n,int *a,int l);
int main()
{
int *a,n,i,l;
scanf(“%d”,&n);
a=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf(“%d”,(a+i));
}
scanf(“%d”,&l);
delete(n,a,l);
return 0;
}
void delete(int n,int *a,int l)
{
int i,j;
if(l<=n)
{
for(i=l-1;i<n;i++)
{
j=i+1;
*(a+i)=*(a+j);
}
printf(“Array after deletion is:\n”);
for(i=0;i<n-1;i++)
{
printf(“%d\n”,(*(a+i)));
}
}
else
{
printf(“Invalid Input”);
}
}

// C++ program to delete an element in an array

#include
using namespace std;

int main()
{
int array[100], position, c, n;

cout << “Enter the number of elements of the array : “;
cin >> n;

cout << “\nInput the array elements : “;

for (c = 0; c < n; c++)
cin >> array[c];

cout << “\nEnter the position : “;
cin >> position;

if (position >= n+1)
cout << “\nDeletion not possible.\n”;
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c+1];

cout << “\nArray after deletion : “;

for (c = 0; c < n – 1; c++)
cout << array[c] << ” “;
}
cout << endl;
return 0;
}

// Java program to delete an element in an array

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, x, flag = 1, loc = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter teh number of elements : “);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Input the array elements : “);
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print(“Enter the element you want to delete:”);
x = s.nextInt();
for (int i = 0; i < n; i++)
{
if(a[i] == x)
{
flag =1;
loc = i;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
for(int i = loc+1; i < n; i++)
{
a[i-1] = a[i];
}
System.out.print(“After Deleting:”);
for (int i = 0; i < n-2; i++)
{
System.out.print(a[i]+”,”);
}
System.out.print(a[n-2]);
}
else
{
}
}
}

# Python program to delete an element in an array

arr = [1,2,3,4,5]

pos = int(input())
arr.remove(arr[pos])

for i in range(0,n-1):
print(arr[i], end = ” “)

## Program to search an element in an array

// C program to search an element in an array

#include <stdio.h>

int main()
{
int array[100], ele, c, n;

printf(“Enter the number of elements of the array : “);
scanf(“%d”, &n);

printf(“\nInput the array elements : “);

for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);

printf(“\nEnter element : “);
scanf(“%d”, &ele);
for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
printf(“\nElement found\n”);
}
}

return 0;
}

#include<stdio.h>
int i,l;
int search(int ,int *,int);
int main()
{
int n,m;
scanf(“%d”,&n);
int a[n];
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
scanf(“%d”,&m);
search(n,a,m);

return 0;
}
int search(int n,int *a,int m)
{
for(i=0;i<n;i++)
{
if(m==a[i])
{
l=1;
break;
}
}
if(l==1)
{
printf(“%d is present in the array”,m);

}
else
{
printf(“%d is not present in the array”,m);
}
}

// C++ program to search an element in an array

#include <iostream>
using namespace std;

int main()
{
int array[100], ele, c, n;

cout << “Enter the number of elements of the array : “;
cin >> n;

cout << “\nInput the array elements : “;

for (c = 0; c < n; c++)
cin >> array[c];

cout << “\nEnter the element : “;
cin >> ele;

for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
cout << “\nElement found\n”;
}
}

return 0;

}

// Java program to search an element in an array

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print(“Enter teh number of elements : “);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Input the array elements : “);
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
int ele = s.nextInt();
for (int i = 0; i < n; i++)
{
if(a[i] == ele)
{
System.out.println(“Element Found”);
}
}
}
}

# Python program to search an element in an array

n = int(input())
sum = 0
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
ele = int(input())
for i in range(0,n):
if(arr[i] == ele):
print(“Element Found”)

Output:

Insert:

Delete:

Search: