Program in Java to find the missing number in a second array

In this tutorial, you will learn how to write Java program to find the missing number in second array.

This program is very simple and easy. As we have fixed array so we are not going to take any array input from the user.

Just execute the program and you will find the missing element in the second array which is available in first array.

Our Logic behind this program is: 

  • We have already two array arr1 with elements 1, 2, 3, 4, and 5.
  • arr2 with elements 2, 3, 1, 0, and 5.
  • We will add the all elements of arr1 together.
  • Similarly for arr2 we will add it.
  • And at last we will subtract  arr1 elements sum with arr2.
  • And we will get the missing number of arr2.

Our program have already two array arr1 and arr2.

arr1 have 1, 2, 3, 4, 5 elements and arr2 have 2,3,1,0,5 elements.

So it should print 4 in output as in second array 4 is missing which is in array first.

Java Program to find missing number in second array which is in first array.

public class Main {  
    public static void main(String[] args) {
    int sum = 0;
    int arr1[]={1,2,3,4,5};
    int arr2[]={2,3,1,0,5};
    int size=arr1.length;
     for(int i=0;i<size;i++){
        sum=sum+arr1[i]-arr2[i];
    }
    System.out.println("missing number is = " +sum);
    }
}   

Output:

java program to find missing number in second array

Leave a Comment