---
title: "Missing Number"  
description: "Missing Number"  
author: "Steilla Mitchel"  
published: 2024-06-12  
updated: 2024-06-12  
canonical: https://www.mindstick.com/forum/160733/missing-number  
category: "c#"  
tags: ["c#", "programming language", "programming help", "programs"]  
reading_time: 2 minutes  

---

# Missing Number

Given an [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) `nums` containing `n` [distinct](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work) numbers in the [range](https://www.mindstick.com/articles/23243/complete-troubleshooting-tips-for-belkin-n300-range-extender-setup) `[0, n]`, return *the only number in the range that is [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) from the array.*

## Example 1:

**[Input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java):** nums = [3,0,1]

**[Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular):** 2

**[Explanation](https://yourviews.mindstick.com/view/84608/what-are-hindenburg-s-allegations-against-adani-detailed-explanation):** n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.

## Example 2:

**Input:** nums = [0,1]

**Output:** 2

**Explanation:** n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.

## Example 3:

**Input:** nums = [9,6,4,2,3,5,7,0,1]

**Output:** 8

**Explanation:** n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.

## Replies

### Reply by Ravi Vishwakarma

Let's write code in C# language to solve the missing number problem.

```cs
// Method to find the missing number in an array of consecutive numbers
public static int MissingNumber(int[] nums)
{
    // Calculate the length of the array
    int N = nums.Length;

    // Calculate the sum of all numbers from 0 to N (inclusive) using the arithmetic series formula
    long TotalSum = N * (N + 1) / 2;

    // Calculate the sum of all numbers in the input array
    long TempSum = 0;
    foreach (var item in nums)
    {
        TempSum += item;
    }

    // Return the difference between the expected sum and the actual sum to find the missing number
    return (int)(TotalSum - TempSum);
}
```

Now call this function from the main function in Program Class.

```cs
    public class Program
    {
        public static void Main()
        {
            IList<int[]> list = new List<int[]>
            {
                new int[] { 3,0,1 },
                new int[] { 0,1 },
                new int[] { 9,6,4,2,3,5,7,0,1 },
            };

            foreach (var item in list)
            {
                Console.WriteLine("Mising number -> " + Program.MissingNumber(item));
            }

            Console.ReadLine();
        }
    }
```

## Output :

```plaintext
Mising number -> 2
Mising number -> 2
Mising number -> 8
```


---

Original Source: https://www.mindstick.com/forum/160733/missing-number

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
