Ask Question
13 December, 13:12

Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.

+1
Answers (1)
  1. 13 December, 16:22
    0
    Since the question expects us to write a method to remove an item from array, we can presume the array built-in method such as removeItem () is not applicable in this context (as this will spoil the purpose of this exercise). Based on this presumption, a method to remove item from array can be as follows (written in Java):

    public static int[] remove (int [] arr, int l, int removeItem) { if (l = = 0) { System. out. println ("The array is empty!"); return arr; } int indexFound = - 1; for (int i = 0; i < l; i++) { if (arr[i] = = removeItem) { indexFound = i; } } if (indexFound = = - 1) { System. out. println ("The remove item is not found in the array"); return arr; }else{ int newArray[] = new int[l-1]; int j = 0; for (int i=0; i < l; i++) { if (i = = indexFound) { continue; } newArray[j] = arr[i]; j++; } return newArray; } } }

    Explanation:

    We start by defining a method called remove () with three expected input, array (arr), length of array (l) and item to be removed (removeItem).

    If the input array is empty (which means the length of array is 0), just print out a message "The array is empty!" and return the original array.

    If not, the program will proceed to check if the removeItem is found in the array by using a for-loop to traverse through each of the number in the array. (Line 10 - 14) If the removeItem is found, we can track the index where the item is found using a variable, indexFound. (Line 12)

    If the removeItem is not found in the array, the initial value of the indexFound will remain - 1 and this can be set as a flag to print out a message "The remove item is not found in the array". (Line 17)

    If found, the program will proceed to create a new array, newArray and copy the values from the original array, arr, to it with the exception of the remove item (Line 20 - 28). The reason we need to create a new array is because Java doesn't allow us to change the size of array after it has been initialized. Therefore, we need to develop an alternative mechanism that copy the values from the original array to the new array.

    At the end, the new array with size reduced by 1 is returned as an output (Line 29).
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers