157k views
0 votes
Write a function removeDuplicates() that gets a list as a parameter, and returns a new list containing the elements of given list where the duplicates are removed. Do not use dictionary

User Secureboot
by
7.4k points

1 Answer

6 votes

Final answer:

To remove duplicates from a list without using a dictionary, you can iterate through the list and add elements to a new list if they are not already present in it.

Step-by-step explanation:

To write a function removeDuplicates() that removes duplicate elements from a given list without using a dictionary, we can use a simple algorithm:

  1. Create an empty list to store the unique elements.
  2. Iterate through each element in the given list.
  3. If the element is not already in the unique elements list, append it.
  4. Return the unique elements list.

Here is an example implementation in Python:

def removeDuplicates(lst):
unique_lst = []
for element in lst:
if element not in unique_lst:
unique_lst.append(element)
return unique_lst

# Example usage
given_lst = [1, 2, 2, 3, 4, 3, 5]
new_lst = removeDuplicates(given_lst)
print(new_lst) # Output: [1, 2, 3, 4, 5]

User David Lavender
by
7.2k points