I have a list of ranges that consist of a start and end value. I need to optimise the list by identifying any overlapping ranges and merging them so that there is no duplication on the list.

I have the following code which works

    private void reconcile() {
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (list.get(j).overlaps(list.get(i))) {
                    list.get(j).merge(list.get(i));
                    list.remove(i);
                    i--;
                }
            }
        }
    }

The list contains custom objects with overlap and merge methods.

As I am removing objects while iterating, I need to include the i-- to ensure I don't skip on object on the list when the indexing changes.

Just wondering if there's a more elegant way of doing this?