Find Missing and Additional Values in Two Lists in Java



In this article, we will learn to find missing and additional values in two lists in Java. By the end of this program, you'll be able to detect elements that exist in one list but not in the other, helping you better manage and analyze data in list comparisons.

Problem Statement

Write a program in Java to find missing and additional values in two lists. Below is the demostration ?

Input

101, 90, 34, 34, 67, 90

Output

The missing element is : 101
The new element in the list is : 67

Steps to find missing and additional values in two lists

Following are the steps to find missing and additional values in two lists ?

  • Create ArrayLists by instantiating my_list_1 and my_list_2.
  • Add elements by using the add method to populate both lists.
  • We will check the missing values by iterating through my_list_1 and use a conditional statement to check if my_list_2 does not contain the current element.
  • After that print the output of the missing element.
  • Check the additional values iterate through my_list_2.
  • Use conditional statement to check if my_list_1 does not contain the current element.
  • Print the output.

Java program to find missing and additional values in two lists

To find missing and additional values in two lists, the Java program is as follows ?

import java.util.*;
public class Demo{
   public static void main(String[] args){
      List<Integer> my_list_1 = new ArrayList();
      List<Integer> my_list_2 = new ArrayList();
      my_list_1.add(101);
      my_list_1.add(90);
      my_list_1.add(34);
      my_list_2.add(34);
      my_list_2.add(67);
      my_list_2.add(90);
      for(int i = 0; i < my_list_1.size(); i++){
         if (my_list_2.contains(my_list_1.get(i)))
         continue;
            else
         System.out.println("The missing element is : "+my_list_1.get(i));
      }
      for(int j=0; j<my_list_2.size();j++){
         if (my_list_1.contains(my_list_2.get(j)))
         continue;
            else
         System.out.println("The new element in the list is : "+my_list_2.get(j));
      }
   }
}

Output

The missing element is : 101
The new element in the list is : 67

Code Explanation

A class named Demo contains the main function and two array lists are created inside it. Elements are added to both the array lists using the add' function. A for loop is used to iterate over the first array list and next, the second array list is checked to contain the elements of the first array list. If that condition is true, then the execution continues. Otherwise, the missing element is figured out and displayed on the console.

Similarly, to check if an additional element that is not present in the first list is present in the second list, the second list is iterated and the extra element (if any) is figured out and displayed on the console.

Updated on: 2024-09-09T01:19:06+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements