Use a helper method for addAndReplaceElement.