Q:

Java program to find missing elements in array elements

belongs to collection: Java Array Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find average of all array elements... >>
<< Java program to find the common strings in two str...