-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraySix.java
46 lines (37 loc) · 1.33 KB
/
ArraySix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//WAP to remove a specific element from an array
import java.util.Scanner;
import java.util.Arrays;
public class ArraySix {
// Remove Elements From The Array
public static void removeElem(int arr[], int elem) {
boolean found = false;
int indextoremove = -1;
// To find the element and it's index
for (int i = 0; i < arr.length; i++) {
if (arr[i] == elem) {
found = true;
indextoremove = i;
break;
}
}
if (found) {
int newarr[] = new int[arr.length - 1];
int moveindex = 0;
for (int i = 0; i < arr.length; i++) {
if (i != indextoremove) {
newarr[moveindex++] = arr[i];
}
}
System.out.println("The new Array after removing " + elem + " is " + Arrays.toString(newarr));
} else {
System.out.println("The element is not found");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = { 25, 14, 56, 15, 36, 56, 77, 18, 29, 49 };
System.out.println("Enter the element you want to remove from the array");
int elem = sc.nextInt();
removeElem(arr, elem);
}
}