Skip to content

๐Ÿง‘๐Ÿพโ€๐Ÿ’ป 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

  1. 1Work out how the current code's running time grows.
  2. 2Suggest a better data structure.
  3. 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
words

Your submission is private. It is marked only against the success criteria โ€” not on whether an AI produced an answer.

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?