×

Java Programs

Java Practice

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. By IncludeHelp Last updated : December 23, 2023

Problem statement

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

Java Array Programs »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.