Understand what the interviewer is asking for by using test cases and questions about the problem.
souvenirs
and an integer threshold
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Count the frequency of each souvenir and filter those below the threshold.
1) Create a dictionary `souvenir_count` to count the frequency of each souvenir.
2) Iterate over the `souvenirs` list to populate the dictionary.
3) Use a list comprehension to filter and return only those souvenirs with a count less than the threshold.
⚠️ Common Mistakes
def declutter(souvenirs, threshold):
# Count the frequency of each souvenir
souvenir_count = {}
for souvenir in souvenirs:
if souvenir in souvenir_count:
souvenir_count[souvenir] += 1
else:
souvenir_count[souvenir] = 1
# Filter souvenirs based on the threshold
decluttered_souvenirs = []
for souvenir in souvenirs:
if souvenir_count[souvenir] < threshold:
decluttered_souvenirs.append[souvenir]
return decluttered_souvenirs