Unit 3 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
lst1
is empty?
lst2
is empty?
lst1
elements appearing in lst2
. But you still need to check if elements occur more than once in lst1
!Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a frequency map of elements across both lists, then add up any element in lst1
with a frequency of 1.
1) Create a frequency map¹ for all elements across both lists
2) For each element in lst1, if the frequency is 1, add to sum
3) Return the sum
¹ see Frequency Count for psuedocode
⚠️ Common Mistakes
def sum_of_unique_elements(lst1, lst2):
count = {}
for num in lst1 + lst2:
if num in count:
count[num] += 1
else:
count[num] = 1
sum_unique = 0
for num in lst1:
if count[num] == 1:
sum_unique += num
return sum_unique