Remove the indexes from the array

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Sample String array
        String[] array = {"A", "B", "C", "D", "E"};

        // Indexes to remove
        int[] indexesToRemove = {1, 3};

        // Remove elements from the array based on indexes
        array = removeElements(array, indexesToRemove);

        // Print the modified array
        System.out.println("Modified Array: " + Arrays.toString(array));
    }

    // Method to remove elements from a String array based on indexes
    public static String[] removeElements(String[] array, int[] indexesToRemove) {
        // Sort the indexes in descending order to prevent shifting issues
        Arrays.sort(indexesToRemove);
        for (int i = indexesToRemove.length - 1; i >= 0; i--) {
            int index = indexesToRemove[i];
            if (index >= 0 && index < array.length) {
                array = removeElementAtIndex(array, index);
            }
        }
        return array;
    }

    // Method to remove an element at a specific index from a String array
    public static String[] removeElementAtIndex(String[] array, int index) {
        if (index < 0 || index >= array.length) {
            return array;
        }
        String[] newArray = new String[array.length - 1];
        int newIndex = 0;
        for (int i = 0; i < array.length; i++) {
            if (i != index) {
                newArray[newIndex] = array[i];
                newIndex++;
            }
        }
        return newArray;
    }
}