Java Program to delete an Element from the Array

Sample Input 1:

5

5 7 9 3 1 9

Sample Output 1:

5 7 3 1

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution

				
			
					
//To Delete An Element From An Array...

import java.util.*;

class Program
{
 public static void main(String args[])
 {
 int i,size,del;
 Scanner sc=new Scanner(System.in);

 System.out.println("Enter The Size:");
 size=sc.nextInt();

 int a[]=new int[size];
 System.out.println("Enter The Array Elements:");
 for(i=0;i<size;i++)
 {
 a[i]=sc.nextInt();
 }

 System.out.println("Enter The Element To Be Deleted:"); 
 del=sc.nextInt();

 for(i=0;i<size;i++)
 {
 if(a[i]==del)
 {

 while(i<size-1)
 { 
 a[i]=a[i+1];
 i++;
 }

 }
 }

 System.out.println("The Output Is:");
 for(i=0;i<size-1;i++)
 {
 System.out.println(" "+a[i]);
 }

 }
}
			
				
			

Program Explanation

Comments