educative.io

Remove duplicate entries in list

This is my program and is not working:
from LinkedList import LinkedList
from Node import Node

def populate_list(empty_set, lst):
for item in empty_set:
print(item)
lst.insert_at_head(item)
return lst

def remove_duplicates(lst):
head = lst.get_head()
empty_set = set()
while head:
empty_set.add(head.data)
head = head.next_element
new_list = LinkedList()
new_list.print_list()
new_list = populate_list(empty_set, new_list)
new_list.print_list()
return new_list
Even if the new_list is not having duplicate, the output is same as the input list


Course: Data Structures for Coding Interviews in Python - Learn Interactively
Lesson: Challenge: Remove Duplicates from Linked List - Data Structures for Coding Interviews in Python

Hi @Naveen1,
In this challenge, you’re asked to return the same lst that was passed as a parameter to the function remove_duplicates. Your code works fine (but it returns a new list instead of returning the same lst that was passed as a parameter). Your code only works fine if we had the following main function in the test cases.

from LinkedList import LinkedList
from Node import Node

def populate_list(empty_set, lst):
    for item in empty_set:
        print(item)
        lst.insert_at_head(item)
    return lst

def remove_duplicates(lst):
    head = lst.get_head()
    empty_set = set()
    while head:
        empty_set.add(head.data)
        head = head.next_element
    
    new_list = LinkedList()
    new_list.print_list()
    new_list = populate_list(empty_set, new_list)
    new_list.print_list()
    return new_list



lst = LinkedList()
lst.insert_at_head(1)
lst.insert_at_head(2)
lst.insert_at_head(4)
lst.insert_at_head(5)
lst.insert_at_head(4)

print("Before")
lst.print_list()
new_list = remove_duplicates(lst)
print("After")
new_list.print_list()

The output for the above code would be

Before
4 -> 5 -> 4 -> 2 -> 1 -> None
After
5 -> 4 -> 2 -> 1 -> None

The output is correct because of the new_list = remove_duplicates(lst) statement.
On the other hand, the main function for the test cases in this challenge is written something like:

lst = LinkedList()

lst.insert_at_head(1)
lst.insert_at_head(2)
lst.insert_at_head(4)
lst.insert_at_head(5)
lst.insert_at_head(4)

print("Before")
lst.print_list()
remove_duplicates(last)
print("After")
lst.print_list()

Output of your code for the above main function is (It is because, the same lst is being printed twice ):

Before
4 -> 5 -> 4 -> 2 -> 1 -> None
After
4 -> 5 -> 4 -> 2 -> 1 -> None

I hope you’re getting my point. Returning a new list is not acceptable for the test cases because of not using the new_list = remove_duplicates(lst) statement. Test cases directly print the same list that was passed to the remove_duplicate(lst) function. That’s why, your solution needs to be modified to return the same lst that was passed to the function.

You can get hints from the Solution provided in the next lesson.

I hope this explanation helps.
Happy Learning :slight_smile: