-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyDeleteArray.java
More file actions
55 lines (30 loc) · 1.51 KB
/
Copy pathmyDeleteArray.java
File metadata and controls
55 lines (30 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
import java.util.Scanner;
public class myDeleteArray{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);// Taking input from user to delete the number
System.out.print("Welcome to myDelete Araay\n ");
int[] numArr = ArrayUtility.inputArray();// Take the elements of an array from the user
System.out.print("Enter the number you want to remove: ");
int numToDelete = input.nextInt();//take the number to delete
int[] newArr = deleteNumber(numArr, numToDelete);//call the method to place the number inside the newArr
System.out.println("Here is your new Array");
ArrayUtility.displayArray(newArr); //diaplay the array traversal
}
public static int[] deleteNumber (int[] numArr , int numToDelete){// method to delete the number input by the user
int occ = OccurencesArray.noOfOccurrence(numArr, numToDelete);//calling the occurrence method
if(occ == 0){ // Here we are checking whether if there is no occurrence of a number then return the same numArr
return numArr;
}
int newSize = numArr.length - occ; // now here we are getting the size of the an array
int[] newArr = new int[newSize]; // created new array
int i = 0 , j = 0 ;
while(i < numArr.length){
if(numArr[i] != numToDelete){
newArr[j] = numArr[i];
j++;
}
i++;
}
return newArr;
}
}