Type-1: 

What is ArrayList?

Below is a Java program that finds the common elements between two ArrayLists of integers:


import java.util.ArrayList;
import java.util.HashSet; 
import java.util.Scanner; 
public class FindCommonElements
public static void main(String[] args) {
// Create two ArrayLists of integers
ArrayList<Integer> list1 = new ArrayList<>(); 
list1.add(5);
list1.add(51);
list1.add(15);
list1.add(52);
list1.add(53);
 ArrayList<Integer> list2 = new ArrayList<>();
list2.add(5); 
list2.add(51);
list2.add(154); 
list2.add(52);
 list2.add(55);
// Find the common elements between the two ArrayLists
ArrayList<Integer> commonElements = findCommonElements(list1, list2);
// Print the common elements 
 System.out.println("Common elements between the two ArrayLists: " + commonElements);
 }
// Method to find common elements between two ArrayLists 
public static ArrayList<Integer> findCommonElements(ArrayList<Integer> list1, ArrayList<Integer> list2) {
 HashSet<Integer> set1 = new HashSet<>(list1);
 HashSet<Integer> set2 = new HashSet<>(list2); 
 set1.retainAll(set2); 
// This modifies set1 to contain only the common elements 
return new ArrayList<>(set1);
 } 
}

Example usage:

Common elements between the two ArrayLists: [51,52,5]

Explanation:

  1. We start by creating two ArrayLists of integers called list1 and list2.
  2. The user is prompted to input elements for both ArrayLists, and the input loop continues until the user enters -1.
  3. The method findCommonElements() takes the two ArrayLists as input and finds the common elements between them using HashSet.
  4. We create two HashSet objects set1 and set2 from list1 and list2, respectively. HashSet automatically removes duplicates and ensures uniqueness of elements.
  5. We use the retainAll() method on set1 to keep only the elements that are present in set2, effectively finding the common elements.
  6. Finally, we convert the common elements from set1 back to an ArrayList and return it.

Using HashSet is an efficient way to find common elements between two ArrayLists, as it ensures that the common elements are unique and removes any duplicates automatically.

Type-2:

Another way to find the common elements between two ArrayLists of integers is by using a simple loop to compare elements. Here's the updated Java program:


import java.util.ArrayList; 
import java.util.Scanner;
public class FindCommonElements {
public static void main(String[] args) {
// Create two ArrayLists of integers
ArrayList<Integer> list1 = new ArrayList<>(); 
list1.add(5);
list1.add(51);
list1.add(15);
list1.add(52);
list1.add(53);
 ArrayList<Integer> list2 = new ArrayList<>();
list2.add(5); 
list2.add(51);
list2.add(154); 
list2.add(52);
list2.add(55);
 // Find the common elements between the two ArrayLists 
 ArrayList<Integer> commonElements = findCommonElements(list1, list2);
// Print the common elements 
 System.out.println("Common elements between the two ArrayLists: " + commonElements);
 } 
// Method to find common elements between two ArrayLists 
public static ArrayList<Integer> findCommonElements(ArrayList<Integer> list1, ArrayList<Integer> list2)
 ArrayList<Integer> commonElements = new ArrayList<>(); 
for (Integer num : list1) {
if (list2.contains(num)) {
 commonElements.add(num); 
 }
 } 
return commonElements;
 }
 }

Example usage:

Common elements between the two ArrayLists: [51,52,5]

Explanation:

  1. We start by creating two ArrayLists of integers called list1 and list2.
  2. The user is prompted to input elements for both ArrayLists, and the input loop continues until the user enters -1.
  3. The method findCommonElements() takes the two ArrayLists as input and finds the common elements between them using a simple loop.
  4. We create a new ArrayList called commonElements to store the common elements found in both lists.
  5. We loop through each element in list1, and for each element, we check if it exists in list2 using the contains() method. If it does, we add it to the commonElements list.
  6. Finally, we return the commonElements list containing the common elements between the two ArrayLists.

This approach works fine for smaller lists. However, keep in mind that its time complexity is O(n * m), where n and m are the number of elements in list1 and list2, respectively. As such, for larger lists, the HashSet-based approach mentioned in the previous solution would be more efficient.