# Find Missing Number from Array in Java

In this post, we will see how to find missing number from array or series of numbers.

## Problem

You have an array of numbers from 1 to n and one number is missing from series.You need to find missing number using java program.

Example:

arr[] = { 1,4,5,3}

Missing number = 2

arr[] = { 1,4,5,3}

Missing number = 2

## Solution

- Calculate sum of number using 1 to N using formula sum1ToN= (N * (N+1))/2
- Calculate sum of all number in array numArrSum
- Missing number = sum1ToN – numArrSum

### Java Program to find missing number in the array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.code2master; public class JavaMissingNumberArray { public static void main(String[] args) { int[] numArr1={1,4,5,3}; System.out.println("Missing number in numArr1: "+getMissingNumberInArray(numArr1)); int[] numArr2={1,4,2,7,3,5}; System.out.println("Missing number in numArr2: "+getMissingNumberInArray(numArr2)); } public static int getMissingNumberInArray(int[] numArr) { int n=numArr.length+1; int sum1Ton = n*(n+1)/2; int numArrSum=0; for (int i = 0; i < numArr.length; i++) { numArrSum+=numArr[i]; } int missingNumber=sum1Ton-numArrSum; return missingNumber; } } |

When you run the above program, you will get below output

Missing number in numArr1: 2

Missing number in numArr2: 6

Missing number in numArr2: 6

That’s all about how to find Missing Number from Array in Java.