29.5k views
5 votes
Write a function called remove duplicates that accepts as a parameter a sorted list of strings and returns a new list excluding duplicates from the original list.

1 Answer

2 votes

Final answer:

The function 'remove_duplicates' accepts a sorted list of strings and returns a new list excluding duplicates by iterating through the original list and keeping a separate list to store unique elements.

Step-by-step explanation:

Writing a function named remove duplicates that accepts a sorted list of strings involves iterating over the original list and adding elements to a new list only if they are not already present. Below is an example of such a function written in Python:


def remove_duplicates(sorted_list):
no_duplicates = []
for item in sorted_list:
if item not in no_duplicates:
no_duplicates.append(item)
return no_duplicates

This function works by maintaining a new list called no_duplicates which is initially empty. As the function iterates through the sorted list, it checks whether the current string is already in no_duplicates. If it is not, the string is appended to no_duplicates. Finally, the new list without duplicates is returned, providing a simple and efficient way to exclude duplicates from a sorted list.