remove last occurrence using 2 ArrayUtils method together

Viewed 122

i am trying to figure out how to use two different ArrayUtils together to solve this. I am unaware of how to combine 2 at once.

Remove the last instance of the given number from the array. Note that the number may occur multiple times in the array, you should only remove the LAST occurrence of the number. You should be able to do this using two different ArrayUtils methods together

  public static int[] removeLastOccurrence(int[] array, int numberToRemove) {


    int [] result = ArrayUtils.remove.ArrayUtils.lastIndexOf(array, numberToRemove);;


    return result;
}
1 Answers

You're close, but it should look more like:

return ArrayUtils.remove(ArrayUtils.lastIndexOf(array, numberToRemove));

However, you might want to add a check to make sure the value exists otherwise you might get an Exception

int idx = ArrayUtils.lastIndexOf(array, numberToRemove);
if (idx >= 0) {
    return ArrayUtils.remove(idx);
}
return array;
Related