Given input integer array arr, and integer num, remove all instances of num from arr. At end of the processing, all elements of arr that are not equal to num should be at the startng indexes of the array. If k instances of num are removed, last n instances of arr should be 0.
| Input Parameters |
Expected outputs |
arr={1,2,3,2,2,6,7}
num=2
|
1,7,3,6,0,0,0
|
arr{ 0,-1,-2,-3,0}
num=0
|
-3,-1,-2,0,0
|
arr={-33,-33,-4,0,77,21,67,-33}
num=-33
|
67,21,-4,0,77,0,0,0
|
| arr={5}
num=5
|
0
|
| The java method should accept following input parameters: arr (int array) and integer num.
|
| Initialise two int variables sIdx=0 (start Index) and eIdx =arr.length-1(end Index).
|
| Iterate the array using while loop while sIdx <= eIdx:
If arr[sIdx] !=num; nothing to be done just increment sIdx and continue to next iteration.
If arr[eIdx]==num;set arr[eIdx] to 0, decrement eIdx and continue to next iteration.
If arr[sIdx]==num:
Set arr[sIdx] to arr[eIdx].
Set arr[eIdx] to 0.
Increment sIdx and decrement eIdx.
|
| At end of while loop all instances of num will be removed and empty indices will be filled with Os at end of arr. Return arr.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayRemoveNumber {
public int[] arrayRemoveNumber(int[] arr, int num) throws Exception{
int sIdx=0; int eIdx=arr.length-1;
while (sIdx <= eIdx) {
if (arr[sIdx] != num) {
sIdx++; continue;
}
if (arr[eIdx]==num) {
arr[eIdx]=0;
eIdx--; continue;
}
if (arr[sIdx] == num) {
arr[sIdx]=arr[eIdx];
arr[eIdx]=0;
sIdx++;
eIdx--;
}
}
return arr;
}
public static void main(String[] args) {
ArrayRemoveNumber ap=new ArrayRemoveNumber();
int[] arr;
try {
int[] intArray1 = {1,2,3,2,2,6,7};
printArraySummary(intArray1, "Original Array");
arr=ap.arrayRemoveNumber(intArray1, 2);
printArraySummary(arr, "Array with "+2+" removed");
int[] intArray2 = {0, -1,-2,-3,0};
printArraySummary(intArray2, "Original Array");
arr=ap.arrayRemoveNumber(intArray2, 0);
printArraySummary(arr, "Array with "+0+" removed");
int[] intArray3 = {-33,-33,-4,0,77,21,67,-33};
printArraySummary(intArray3, "Original Array");
arr=ap.arrayRemoveNumber(intArray3, -33);
printArraySummary(arr, "Array with "+-33+" removed");
int[] intArray4 = {5};
printArraySummary(intArray4, "Original Array");
arr=ap.arrayRemoveNumber(intArray4, 5);
printArraySummary(arr, "Array with "+5+" removed");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}