69.9k views
1 vote
Banish. Write a method named banishthat accepts two arrays of integers a1 and a2 as parameters and removes all occurrences of a2's values from a1.

An element is "removed" by shifting all subsequent elements one index to the left to cover it up, placing a 0 into the last index.

The original relative ordering of a1's elements should be retained. For example, suppose the following two arrays are declared and the following call is made:

int[] a1 = {42, 3, 9, 42, 42, 11, 42, 9, 42, 42, 17, 0, 8, 2222, 4, 9, 0, 1}; int[] a2 = {42, 2222, 9}; banish(a1, a2);

After the call has finished, the contents of a1 should become: [3, 11, 17, 0, 8, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Notice that all occurrences of the values 42, 2222, and 9 have been removed and replaced by 0s at the end of the array, and the remaining values have shifted left to compensate.

2 Answers

3 votes

Answer:

The solution code is written in Java

  1. public static void banish(int []arr1, int[]arr2){
  2. for(int i =0; i < arr2.length; i++){
  3. for(int j = 0; j < arr1.length; j++){
  4. if(arr1[j] == arr2[i]){
  5. arr1[j] = 0;
  6. }
  7. }
  8. }
  9. int cursor = 0;
  10. for(int k = 0; k < arr1.length; k++){
  11. if(arr1[k] != 0){
  12. arr1[cursor] = arr1[k];
  13. arr1[k] = 0;
  14. cursor += 1;
  15. }
  16. }
  17. }

Step-by-step explanation:

The main logic of this banish can be divided into two parts:

First part (Line 1 -8):

This part of code is to replace all occurrence of a2 elements in the a1 array with zeros. The key idea is to use for loop to traverse through each element in a1 and check the a1 element against each of the a2 element. If any match is found, replace the a1 element with 0.

Second part (Line 10 - 18):

This part of code is to push all the non-zero values in a1 to the front. Firstly, set a cursor with initial value 0. Next, use a for loop to traverse through the a1 array to check for the non-zero element. If any non-zero element is found, use the cursor as an index to extract the element and replace it with the non-zero element. The original element addressed by k is replaced with zero and then increment the cursor by 1.

User Duarte Harris
by
5.5k points
2 votes

Answer:

import java.lang.*; //Importation of Java Language Package

import java.util.*; //Importation of Java Utilities Package

import sun.security.util.Length; //Importation of Utility.length Package

//Which grants us access to the length of

// the array

class QuickStart { //Start of class definition

//start of array shifting function definition.

public static int[] banish (int a1[], int a2[]) {

// Start of loop through array

For(int i = 0; i < a1.length; i++){

//Start of loop through the second array

for(int j = 0; j < a2.length; j++){

if(a1[i] == a2[j]){ //A check

//if the element in the first

//is equal to the element in the second

for(int k = i; k< a1.length - 1 ; k++ ){ //This is where the

a1[k] =a1[k+1]; // next to element

//is shifted into

//the element to

//removed.

} //End of for loop that shifts the elements

a1[a1.length-1] = 0; // This is where 0 is added to the end of

// the array to make up for the removed

// array.

} //End of if Statement that checks for a match

} //End of for loop that loops through the second array

} // End of for loop that loops through the first element

return a1; //This is where the resulting array is returned to the

// main program

} //End of array shifting function definition.

/**************************Start of Main function***********************/

public static void main(String[] args)

{

int[] intArr = {42, 3, 9, 42, 42, 11, 42, 9, 42, 42, 17, 0, 8, 2222, 4, 9 , 0, 1};//Definition of the first array

int[] intArr2 = {42, 2222, 9};//Definition of second array

//This is where the returned array is displayed

System.out.println(Arrays.toString(banish (intArr, intArr2)));

}// End of Main function

}//End of Class

Step-by-step explanation:

The Programing Challenge was to remove all the elements in array2 from array1 in such a way that an element is "removed" by shifting all subsequent elements one index to the left to cover it up, placing a 0 into the last index. To find solution to this challenge we first define a function (Named banish) that will take in two array arguments.

Within that function we create a (for loop operator) which will loop through all the elements of the first array, this is done so that we can be able to compare each elements of the first array with that of the second element. Next we create another (for loop operator) within the first for loop that will loop through the second array, this is done so that each element of the first array can be compared with every element of the second array. Next using an (if statement) a check is carried out determine if an element of the first array is equal to any element of the second element, and if it is the then the next the program does is to create a for loop within the if statement, what this for loop does is to shift the element that is after the element (i.e. the element in the first array that matches the element in the second array) into the index position of that element(i.e. the element in the first array that matches the element in the second array).to replace the element.

for(int k = i; k< a1.length - 1 ; k++ ){

a1[k] =a1[k+1];

Then it continues shifting other preceding elements to fill the gap left by the previous element until it reaches the end of the array. The next thing the program does after it exist this last for loop is to shift 0 into the last position of the first array to make up for the replaced element. It then return to the first for loop in order to carry out the same process on the next element of the first array. When all the elements of the first array have been checked then the function (banish) is made to return the modified first array to the main program. At the main program the returned array from the function(banish) is displayed.

User Ben Bryant
by
5.0k points