Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
System.out.print("Enter the size of an array: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
System.out.print("Enter " + (i + 1) + " element: ");
a[i] = sc.nextInt();
}
System.out.print("Enter the element which you want to remove from array: ");
int no = sc.nextInt();
int flag = 0;
for (int c = 0; c < a.length; c++) {
if (a[c] == no) {
a[c] = a[c + 1];
for (int j = c + 1; j < a.length; j++) {
if (j < n - 1) {
a[j] = a[j + 1];
} else {
a[j] = 0;
flag++;
}
}
}
}
for (int b = 0; b < a.length - flag; b++) {
System.out.println("a[" + b + "]=" + a[b]);
}
}
}
Output:
Tags:
Java Programs