C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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








COMMENTS