---
title: "Given an array of non-duplicating numbers from 1 to n where one number is missing, find it."  
description: "Given an array of non-duplicating numbers from 1 to n where one number is missing, find it."  
author: "Mukul Goenka"  
published: 2021-11-22  
updated: 2023-04-28  
canonical: https://www.mindstick.com/forum/156859/given-an-array-of-non-duplicating-numbers-from-1-to-n-where-one-number-is-missing-find-it  
category: "java"  
tags: ["c#", "java", "programming language"]  
reading_time: 3 minutes  

---

# Given an array of non-duplicating numbers from 1 to n where one number is missing, find it.

Given an [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) of non-duplicating numbers from 1 to n where one number is [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on), write an [efficient](https://www.mindstick.com/articles/33637/three-tips-for-an-energy-efficient-home-for-2019) [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to find that missing number.

## Replies

### Reply by Aryan Kumar

o find the missing number in an array of non-duplicating numbers from 1 to n, you can use the following approach:

1. Calculate the sum of all the numbers from 1 to n using the formula: **sum = n * (n + 1) / 2**
2. Calculate the sum of all the numbers in the array
3. Subtract the sum of the array from the sum of all the numbers from 1 to n. The difference will be the missing number.

Here's a sample code snippet in Java that implements this approach:

```java
public class FindMissingNumber {
   public static int findMissing(int[] arr, int n) {
       int sum = n * (n + 1) / 2;
       int arrSum = 0;
       for (int i = 0; i < arr.length; i++) {
           arrSum += arr[i];
       }
       return sum - arrSum;
   }
   public static void main(String[] args) {
       int[] arr = {1, 2, 4, 5, 6};
       int n = 6;
       int missingNumber = findMissing(arr, n);
       System.out.println("The missing number is: " + missingNumber);
   }
}
```

In this example, we have an array of numbers from 1 to 6, with one number missing (3). We call the **findMissing()** method, passing in the array and the value of n (which is the length of the array plus one).

The **findMissing()** method calculates the sum of all the numbers from 1 to n using the formula **sum = n * (n + 1) / 2**, then calculates the sum of the array using a loop. The missing number is then calculated by subtracting the sum of the array from the sum of all the numbers from 1 to n.

### Reply by Krishnapriya Rajeev

The code given below shows how to find the missing number in a non-duplicating array of numbers:

```plaintext
class missingNum
{
    public static void main(String[] args)
    {
        int[] nums = { 1, 2, 3, 5, 4, 8, 6};	// Declare and initialize and an integer array.
        int n = nums.length;	// Store length of the integer array
        int sum = ((n + 1) * (n + 2)) / 2;	// Sum of n numbers
        for (int i = 0; i < n; i++)
            sum -= nums[i];		// Subtracts each element from sum
         System.out.println("The missing number is " + sum + ".");
    }
}
OUTPUT:
The missing number is 7.
```

We can obtain the missing element by subtracting the sum of all elements from the sum of first n natural numbers, where n is the largest number in the array.

### Reply by Ravi Vishwakarma

```
class HelloWorld {   public static void main(String[] args) {       int[] array={4,3,8,7,5,2,6};       int missingNumber = findMissingNum(array);       System.out.println('Missing Number is '+ missingNumber);    }   public static int findMissingNum(int[] array) {       int n=array.length+1;       int sumOfFirstNNums=n*(n+1)/2;       int actualSumOfArr=0;       for (int i = 0; i < array.length; i++) {           actualSumOfArr+=array[i];       }       return sumOfFirstNNums-actualSumOfArr;   }}
```

Output.

Missing Number is 1


---

Original Source: https://www.mindstick.com/forum/156859/given-an-array-of-non-duplicating-numbers-from-1-to-n-where-one-number-is-missing-find-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
