int[] removeRegions(int[] regions, Set<Integer> regionIndicesToRemove) {
    // Calculate the size of the new regions array
    int newSize = regions.length - regionIndicesToRemove.size();
    if (newSize < 0) {
      throw new IllegalStateException(
        "Region indices mismatch: more regions to remove than in the regions array");
    }

    int[] newRegions = new int[newSize];
    int newIndex = 0;

    // Copy only the regions not in the removal set
    for (int region : regions) {
      if (!regionIndicesToRemove.contains(region)) {
        newRegions[newIndex++] = region;
      }
    }

    // If the newIndex is smaller than newSize, some regions were missing from the input array
    if (newIndex != newSize) {
      throw new IllegalStateException("Region indices mismatch: some regions in the removal "
        + "set were not found in the regions array");
    }

    return newRegions;
  }
Common method for per host and per Location region index updates when a region is moved.
@param serverIndexToLocation             serverIndexToHostIndex or serverIndexToLocationIndex
@param regionsPerLocation                regionsPerHost or regionsPerLocation
@param colocatedReplicaCountsPerLocation colocatedReplicaCountsPerHost or
                                         colocatedReplicaCountsPerRack
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/BalancerClusterState.java