I need to find the difference between two lists for a problem i am trying to solve.
For example if:
list1 = ["Johny", "Lisa", "Madison", "Kirean"]
list2 = ["Lisa", "Madison", "Kirean"]
print difference(list1, list2)
#Should return ["Johnny"]
I have tried two different methods.
One was looping through the list and checking for similar elements and removing similar elements. Which took too long to compile for half the cases.
The other way was using subtracting sets. But this approach returned nothing in some of the cases.(Those cases were solved by the first solution if they were within the time limit.)
Is there any other way that would be invincible and also fast enough?
This is the link to the problem: http://www.dmoj.ca/problem/coci14c2p2
And here is my code:
a = []
b = []
n = input()
for i in range(n):
a.append(raw_input())
for i in range(n-1):
b.append(raw_input())
print list(set(a) ^ set(b))[0]