forked from FatimaAlazazi/DataStructure2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveElmentFromArray.java
More file actions
59 lines (50 loc) · 1.58 KB
/
RemoveElmentFromArray.java
File metadata and controls
59 lines (50 loc) · 1.58 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
package Task2;
import java.util.Scanner;
public class RemoveElmentFromArray {
static int [] a = {7,2,4,5,6,3,1};
static Scanner in = new Scanner(System.in);
static int [] temp = new int[a.length-1] ;
static public void Print()
{
System.out.print("[ ");
for (int i = 0; i <a.length ; i++) {
if(i==a.length-1)
System.out.print(a[i]+" ]");
else
System.out.print(a[i]+" , ");
}
}
static public void Remove(int x)
{
int y ;
for (int i = 0 , k=0 ; i <a.length ; i++) {
if(a[i]==x)
{
continue;
}
temp[k++]=a[i];
}
}
static public void PrintRemovedArray()
{
System.out.print("[ ");
for (int i = 0; i <temp.length ; i++) {
if(i==temp.length-1)
System.out.print(temp[i]+" ]");
else
System.out.print(temp[i]+" , ");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Elements Before Remove : ");
Print();
System.out.println("\n============================================");
System.out.println("Enter the Element you want to remove : ");
int x = in.nextInt();
Remove(x);
System.out.println("==============================================");
System.out.println("Elements After Remove : ");
PrintRemovedArray();
}
}