๐ง๐พโ๐ป Make it faster
Intermediate
Analyse
๐ป Computational Thinking
๐ค AI Thinking and AI Literacy
โฑ about 15 minutes
10 points
The challenge
This Python function checks whether a list has any duplicate value. It works, but it is slow for large lists. Propose a better approach, show the code, and explain why it is faster.
def has_duplicates(items):
for i in range(len(items)):
for j in range(i + 1, len(items)):
if items[i] == items[j]:
return True
return False
Instructions
- 1Work out how the current code's running time grows.
- 2Suggest a better data structure.
- 3Show the improved code and explain the complexity.
Rules
- Your code must give the same results as the original.
Success criteria
Your submission is marked against these criteria only.
- Uses a set or dictionary Checked automatically 3 pts
- States the complexity Checked automatically 2 pts
- Shows improved code Checked automatically 3 pts
- Improvement is correct and explained Needs AI review โ not configured yet, so not assessed 2 pts
What an AI replied to your prompt
For comparison only โ this reply does not affect your score. Does it give you what you asked for?