Find missing number in a sequence in C#

Finding missing numbers in a sequence is a common programming problem. In C#, you can solve this efficiently using LINQ operations by comparing the original sequence with a complete range of numbers.

Using LINQ Except Method

The most straightforward approach is to create a complete range from the minimum to maximum value and use the Except method to find missing numbers −

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
   public static void Main() {
      List<int> myList = new List<int>(){1, 2, 3, 5, 8, 9};
      
      Console.WriteLine("Original Numbers:");
      foreach(int val in myList) {
         Console.WriteLine(val);
      }
      
      // Get the first and last elements
      int first = myList.Min();
      int last = myList.Max();
      
      // Create complete range and find missing numbers
      List<int> completeRange = Enumerable.Range(first, last - first + 1).ToList();
      List<int> missingNumbers = completeRange.Except(myList).ToList();
      
      Console.WriteLine("Missing Numbers:");
      foreach (int missing in missingNumbers) {
         Console.WriteLine(missing);
      }
   }
}

The output of the above code is −

Original Numbers:
1
2
3
5
8
9
Missing Numbers:
4
6
7

Using HashSet for Better Performance

For larger sequences, using a HashSet provides better performance with O(1) lookup time −

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
   public static void Main() {
      List<int> numbers = new List<int>(){10, 12, 14, 17, 20};
      
      HashSet<int> numberSet = new HashSet<int>(numbers);
      int min = numbers.Min();
      int max = numbers.Max();
      
      Console.WriteLine("Original sequence: " + string.Join(", ", numbers));
      Console.Write("Missing numbers: ");
      
      List<int> missing = new List<int>();
      for (int i = min; i <= max; i++) {
         if (!numberSet.Contains(i)) {
            missing.Add(i);
         }
      }
      
      Console.WriteLine(string.Join(", ", missing));
   }
}

The output of the above code is −

Original sequence: 10, 12, 14, 17, 20
Missing numbers: 11, 13, 15, 16, 18, 19

Finding Missing Number in Consecutive Sequence

For finding a single missing number in a consecutive sequence starting from 1, you can use the mathematical approach −

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
   public static void Main() {
      List<int> sequence = new List<int>(){1, 2, 3, 5, 6, 7, 8};
      
      int n = sequence.Count + 1; // Expected length
      int expectedSum = n * (n + 1) / 2;
      int actualSum = sequence.Sum();
      int missingNumber = expectedSum - actualSum;
      
      Console.WriteLine("Sequence: " + string.Join(", ", sequence));
      Console.WriteLine("Missing number: " + missingNumber);
   }
}

The output of the above code is −

Sequence: 1, 2, 3, 5, 6, 7, 8
Missing number: 4

Comparison of Methods

Method Time Complexity Best Use Case
LINQ Except O(n) Multiple missing numbers in any range
HashSet Lookup O(n) Large sequences with better performance
Mathematical Sum O(n) Single missing number in consecutive sequence

Conclusion

Finding missing numbers in a sequence can be achieved through various approaches in C#. Use LINQ's Except method for simplicity, HashSet for better performance with large datasets, or mathematical calculations for single missing numbers in consecutive sequences.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements