Some suggestions, added as comments:
function filter_the_comments( $array ) {
$comment_IDs = array();
foreach ( $array as $key => $val ) {
//assuming the comments are sorted by date in ascending order, a parent has to come before the child comment, so there is no need to wait till all subsequent IDs have been added in order to check for this comment's parent
//the && operator stops executing as soon as a condition fails, so it's more efficient to check if there is a parent first
//0 evaluates to false and other values to true, so the comparison can be skipped
if ($val->comment_parent && !in_array( $val->comment_parent, $comment_IDs )) unset( $array[ $key ] );
//there is no need to add this comment's ID if you have removed it, so put this in the else statement
else array_push($comment_IDs, $val->comment_ID);
}
return $array;
};