-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplete_remove.java
45 lines (38 loc) · 1.24 KB
/
complete_remove.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
//Code to completely remove any number from an array
package Java;
import java.util.Scanner;
public class complete_remove {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the size of an array");
int n = sc.nextInt();
int numbers[] = new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
numbers[i] = sc.nextInt();
}
System.out.println("Enter the number to remove");
int numbertoremove = sc.nextInt();
System.out.println("Original Array");
for(int i=0;i<n;i++)
{
System.out.print(numbers[i] + " ");
}
int newsize = 0;
for(int i=0;i<numbers.length;i++)
{
if(numbers[i] != numbertoremove){
numbers[newsize] = numbers[i];
newsize++;
}
}
System.out.println();
System.out.println("Resultant Array");
for(int i=0;i<newsize;i++)
{
System.out.print(numbers[i] + " ");
}
}
}
}