Home » Java programs » Java Array programs

Java program to find missing elements in array elements



In this java program, we are going to learn how to find missing element from array elements? Here, we have an array and finding an element which is missing in the list.
Submitted by IncludeHelp, on November 30, 2017

Given an array of integers (in a series) and we have to find its missing elements (there will be a missing element) using java program.

Example:

    Input array: 1, 2, 3, 4, 6, 7
    Output:
    Missing element is: 5

Program to find missing element in an array in java

public class ExArrayMissing 
{ 
	public static void main(String[] args) 
	{
		// initialize here.
	    int n;
	    
	    // take default input array.
	    int[] numbers = new int[]{1,2,3,4,6,7};
	    
	    // last elements.
	    n = 7;
	    
	    // sum of elements till last value.
	    int expected_sum = n * ((n + 1) / 2);
	    int sum = 0;
		
	    for (int i: numbers)
	    {
		    sum += i;
	    }
	    
		// obtain missing elements.
	    int m=expected_sum - sum;
	    System.out.print("Missing element is : " +m);
	    System.out.print("\n");
	}
}

Output

    Missing element is : 5





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.