refactored third method that removes all nil elements from an array