-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteFromArray.java
More file actions
50 lines (30 loc) · 1.1 KB
/
Copy pathDeleteFromArray.java
File metadata and controls
50 lines (30 loc) · 1.1 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
import java.util.Scanner;
public class DeleteFromArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welocome to Array Deletion\n");
int[] numArr = ArrayUtility.inputArray();
System.out.print("Now Enter the number you want to delete: ");
int numToDelete = input.nextInt();
int[] newArr = deleteNumber(numArr, numToDelete);
System.out.println("Here is your new Array: ");
ArrayUtility.displayArray(newArr);//Display Array from ArrayUtlity Method
}
public static int[] deleteNumber(int[] numArr,int numToDelete){
int occ = OccurencesArray.noOfOccurrence(numArr, numToDelete);
if(occ == 0){
return numArr;
}
int newSize = numArr.length - occ;
int[] newArr = new int[newSize];
int i = 0 , j = 0;
while(i < numArr.length){
if(numArr[i] != numToDelete){
newArr[j] = numArr[i];
j++;
}
i++;
}
return newArr;
}
}