1// Java program to delete specified integer from an array
2import java.util.Scanner;
3public class DeleteSpecifiedInteger
4{
5 public static void main(String[] args)
6 {
7 int num, n, temp = 1, place = 0;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Please enter number of elements: ");
10 num = sc.nextInt();
11 int[] arrNum = new int[num];
12 System.out.println("Please enter all the elements: ");
13 for(int a = 0; a < num; a++)
14 {
15 arrNum[a] = sc.nextInt();
16 }
17 System.out.println("Enter the element you want to delete: ");
18 n = sc.nextInt();
19 for(int a = 0; a < num; a++)
20 {
21 if(arrNum[a] == n)
22 {
23 temp = 1;
24 place = a;
25 break;
26 }
27 else
28 {
29 temp = 0;
30 }
31 }
32 if(temp == 1)
33 {
34 for(int a = place + 1; a < num; a++)
35 {
36 arrNum[a - 1] = arrNum[a];
37 }
38 System.out.println("After deleting element: ");
39 for(int a = 0; a < num - 2; a++)
40 {
41 System.out.print(arrNum[a] + ",");
42 }
43 System.out.print(arrNum[num - 2]);
44 }
45 else
46 {
47 System.out.println("Element not found!!");
48 }
49 sc.close();
50 }
51}
1import java.util.Scanner;
2
3public class ElemRemoval {
4
5 public static void main(String[] args) {
6 Scanner in = new Scanner(System.in);
7 int[] intArr = {1, 2, 5, 12, 7, 3, 8};
8 System.out.print("Enter Element to be deleted : ");
9 int elem = in.nextInt();
10
11 for(int i = 0; i < intArr.length; i++){
12 if(intArr[i] == elem){
13 // shifting elements
14 for(int j = i; j < intArr.length - 1; j++){
15 intArr[j] = intArr[j+1];
16 }
17 break;
18 }
19 }
20
21 System.out.println("Elements -- " );
22 for(int i = 0; i < intArr.length - 1; i++){
23 System.out.print(" " + intArr[i]);
24 }
25 }
26}