logo CodeStepByStep logo

banish

Language/Type: PHP arrays

Write a function named banish that 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 at 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:

$a1 = [42, 3, 9, 42, 42, 0, 42, 9, 42, 42, 17, 8, 2222, 4, 9, 0, 1];
$a2 = [42, 2222, 9];
banish($a1, $a2);

After the call has finished, the contents of $a1 should become:

[3, 0, 17, 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.

Do not make any assumptions about the length of the arrays or the ranges of values each might contain. For example, each array might contain no elements or just one element, or very many elements. (If $a2 is an empty array that contains no elements, $a1 should not be modified by the call to your function.) You may assume that the arrays passed are not NULL. You may assume that the values stored in $a2 are unique and that $a2 does not contain the value 0.

A note about references in PHP: In order to write a function passes a parameter as reference (thus modifying its state), you'll need to prepend "&" to the variable declaration in the function header. For example, a function foo that modifies the state of an array parameter may be defined as:

function foo(&$arr) { ... }

Constraints:

  • You should not use any temporary arrays or strings to help you solve this problem (but you may declare as many simple variables as you like, such as integers.)
Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.