Optimized remove_by_value function