Remove element from an array by index

 Problem statement:- Program to remove an element from an array by index.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[4,5,7,3,9]  

                                 Position=2

                               /*Index[0]=4

                                Index[1]=5

                                Index[2]=7

                                Index[3]=3

                                 Index[4]=9*/

                               

             Output:

                       arr[]= [4,5,3,9]          

Data requirement:-

   Input Data:- size, arr, pos

  Output Data:-arr

  Additional Data:- i

Program in C

Here is the source code of the C Program to remove an element from an array by index.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size,i;
    scanf("%d",&size);
    int arr[size];

    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int pos;
    printf("\nEnter the position of the Element:");
    scanf("%d",&pos);

    printf("\nBefore deleting array elements are:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    pos+=1;

         for(i=pos-1;i<size-1;i++)
            arr[i] = arr[i+1];

     printf("\nAfter Deleting Array Element are:");
    for(i=0;i<size-1;i++)
    {
        printf("%d ",arr[i]);
    }
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4 5 7 3 9

Enter the position of the Element:2

Before deleting array elements are:4 5 7 3 9
After Deleting Array Element are:4 5 3 9

Program in C++

Here is the source code of the C++ Program to remove an element from an array by index.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size,i;
    cin>>size;
    int arr[size];

    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }

    int pos;
    cout<<"\nEnter the position of the Element:";
    cin>>pos;
    cout<<"\nBefore deleting array elements are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
        pos+=1;

         for(i=pos-1;i<size-1;i++)
            arr[i] = arr[i+1];

    cout<<"\nAfter Deleting Array Element are:";
    for(i=0;i<size-1;i++)
    {
        cout<<arr[i]<<" ";
    }
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
4 3 9 7 5 10

Enter the position of the Element:3

Before deleting array elements are:4 3 9 7 5 10
After Deleting Array Element are:4 3 9 5 10

Program in Java

Here is the source code of the Java Program to remove an element from an array by index.

Code:

import java.util.Scanner;
public class DeleteElement {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      int pos;
      System.out.print("\nEnter the position of the Element:");
      pos=sc.nextInt();
      
      System.out.print("\nBefore deleting array elements are:");
      for(i=0;i<size;i++)
      {
       System.out.print(arr[i]+" ");
      }
      
      pos+=1;

         for(i=pos-1;i<size-1;i++)
            arr[i] = arr[i+1];
         
      System.out.print("\nAfter Deleting Array Element are:");
      for(i=0;i<size-1;i++)
      {
       System.out.print(arr[i]+" ");
      }
      sc.close();
}
}

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
3 12 14 15 

Enter the position of the Element:3

Before deleting array elements are:3 12 14 15 
After Deleting Array Element are:3 12 14 

Program in Python

Here is the source code of the Python Program to remove an element from lists by index.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
pos=int(input("Enter the position of the Element:"))
print("Before deleting array elements are:")
for i in range(0,size):
    print(arr[i],end=" ")

arr.pop(pos)
print("\nAfter Deleting Array Element are:")
print(arr)

Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
5
8
3
6
Enter the position of the Element:3
Before deleting array elements are:
5 8 3 6 
After Deleting Array Element are:
[5, 8, 3]

Post a Comment

0 Comments