-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteFromArray.java
More file actions
59 lines (48 loc) · 1.51 KB
/
deleteFromArray.java
File metadata and controls
59 lines (48 loc) · 1.51 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
import java.util.Scanner;
public class deleteFromArray {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Welcome to Array Deletion \n");
int[] numArray = arrayInputUtility.inputArray();
System.out.print("Enter own Num for Deletion : ");
int deletedNum = input.nextInt();
int[] AfterDeletionArray = deletedNumArray(numArray, deletedNum);
System.out.println("Below is your New Array");
DisplayArray(AfterDeletionArray);
}
public static int [] deletedNumArray (int[]Array, int Num){
// Step 1 (count same value)
int count = 0;
int i = 0;
while (i < Array.length) {
if (Array[i] == Num) {
count ++;
}
i++;
}
if (count ==0) {
return Array;
}
// Step 2 (New Array Declare)
int[] NewArray = new int[Array.length - count];
// Step 3 (Array index value assign)
int index = 0;
int NewArrayindex = 0;
while (index < Array.length) {
if (Array[index] != Num) {
NewArray[NewArrayindex] = Array[index];
NewArrayindex++;
}
index++;
}
return NewArray;
}
public static void DisplayArray(int[] Array){
int i = 0;
while (i < Array.length) {
System.out.print(Array[i] + " ");
i++;
}
System.out.println();
}
}