DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 6: The Case of the Missing Number, Sets to the Rescue!

Welcome to my Challenge #6 in the #80DaysOfChallenges journey!
Today's challenge is a beautiful example of how elegant Python can be when you know your data structures. We're going to use sets to find a missing number in a list of consecutive integers, clean, efficiently, and with zero fuss.


💡 Objective: Using Set Difference to Find What's Missing

Here's the setup:
You're given a list of numbers from 1 to n, but one number is missing. The task is to find which one.

You could loop through and compare each element, or sum everything and subtract, but that's too noisy.
Instead, let's let set theory do the heavy lifting.

Sets in Python act like mathematical sets, unique elements, lightning-fast lookups, and built-in comparison operations like union, intersection, and difference.


🧩 The Thinking Behind the Solution

Imagine you have numbers 1 to 10, but 5 went missing.
We can build two sets:

  • expected_numbers: {1, 2, 3, ..., 10}, what we should have
  • given_numbers: the list were actually given Then we take the set difference (expected_numbers - given_numbers). What remains is exactly what's missing: {5}.

Elegant. Minimal. Mathematical.


🔍 The Final Solution

def locate_missing_value(sequence: list[int]) -> int:
    """
    locate_missing_value finds the single missing element 
    in a sequence of consecutive integers starting from 1.
    """
    total_count = len(sequence) + 1  # Total numbers including the missing one
    expected_numbers = set(range(1, total_count + 1))
    given_numbers = set(sequence)
    difference = expected_numbers - given_numbers
    return difference.pop()  # Return the missing number
Enter fullscreen mode Exit fullscreen mode

Example Run

data = [1, 2, 3, 4, 6, 7, 8, 9, 10]
print(locate_missing_value(data))
Enter fullscreen mode Exit fullscreen mode

Output:

5
Enter fullscreen mode Exit fullscreen mode

Yep, exactly what we expected.


⚙️ Why This Approach Shines

  1. Logical simplicity: No complicated loops or conditions. Just math and sets.
  2. Speed: Set operations are implemented with hash tables under the hood, they run in near-constant time, much faster than linear searches.
  3. Scalability: If one day your list could have multiple missing numbers, this same method still works. The difference operation will just return all missing values.

🎯 Key Takeaway

This challenge is a neat demonstration of how thinking in sets can simplify logic-heavy problems.
Sets are ideal for comparison, deduplication, and difference detection, the perfect tools when you need clean, declarative code that mirrors mathematical reasoning.

In future challenges, we can expand this idea:
What if there are duplicates? What if numbers aren’t consecutive? Each twist pushes us closer to understanding Python’s data structures at a deeper level.


🚀 Ready for the Next Step?

Now it’s your turn.
Try modifying the function to handle multiple missing numbers, or even detect duplicates in the list.
Once you’ve got it working, share your version online using #80DaysOfChallenges. Let’s see how your brain approaches the same problem.

Want to inspire other learners? Add your twist, post your code snippet, and tag a friend to join the challenge.


Challenge Resources

You can find the full source code for today's challenge on GitHub.

• Source Code for Challenge #6: scripts/missing_number_in_sequence.py
• Main Repository: 80-days-of-challenges
• Daily Updates: Twitter/X (@Shahrouzlogs)

Top comments (0)