-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteFromArray.java
More file actions
34 lines (30 loc) · 1.04 KB
/
Copy pathDeleteFromArray.java
File metadata and controls
34 lines (30 loc) · 1.04 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
import java.util.Scanner;
public class DeleteFromArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("welcome to array deletion\n");
int[] numArr = ArrayUtility.inputArray();
System.out.println("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);
}
public static int[] deleteNumber(int[] numArr,int numToDelete) {
int occ = OccurrencesArray.noOfOccurrences(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;
}
}