Given two arrays, the task is to find the elements that are present in both arrays. Each common element is included only once in the result.
- Find elements that are present in both arrays.
- HashSet can be used to store and find common elements efficiently.
Example
Input: Â Array1 = ["Article", "for", "Geeks", "for", "Geeks"],Â
      Array2 = ["Article", "Geeks", "Geeks"]
Output: [Article, Geeks]Input: Â Array1 = ["a", "b", "c", "d", "e", "f"],Â
      Array2 = ["b", "d", "e", "h", "g", "c"]
Output: [b, c, d, e]
Different Approaches to Find Common Elements
1. Using Nested Loops
In this approach, each element of the first array is compared with every element of the second array. When a match is found, the element is added to a HashSet.
import java.util.HashSet;
import java.util.Set;
public class Geeks {
public static void findCommonElements(String[] arr1, String[] arr2) {
Set<String> common = new HashSet<>();
// Compare each element of arr1 with arr2
for (String element1 : arr1) {
for (String element2 : arr2) {
if (element1.equals(element2)) {
common.add(element1);
break;
}
}
}
System.out.println(common);
}
public static void main(String[] args) {
String[] arr1 = {"Article", "in", "Geeks", "for", "Geeks"};
String[] arr2 = {"Geeks", "for", "Geeks"};
findCommonElements(arr1, arr2);
}
}
Output
[Geeks, for]
Explanation
- Two arrays are created.
- The outer loop selects each element from arr1.
- The inner loop compares it with every element of arr2.
- If both elements are equal, the element is added to the HashSet.
- HashSet automatically removes duplicate entries.
- break avoids unnecessary comparisons after a match is found.
2. Using HashSet and retainAll()
The retainAll() method keeps only the elements that are common to two collections.
Approach
- Add the elements of the first array to a HashSet.
- Add the elements of the second array to another HashSet.
- Call retainAll() on the first set.
- The first set now contains only the common elements.
import java.util.HashSet;
import java.util.Set;
public class Geeks {
public static void findCommonElements(int[] arr1, int[] arr2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
// Add elements of the first array
for (int value : arr1) {
set1.add(value);
}
// Add elements of the second array
for (int value : arr2) {
set2.add(value);
}
// Keep only common elements
set1.retainAll(set2);
System.out.println(set1);
}
public static void main(String[] args) {
int[] arr1 = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
int[] arr2 = {100, 9, 64, 7, 36, 5, 16, 3, 4, 1};
findCommonElements(arr1, arr2);
}
}
Output
[16, 64, 1, 4, 36, 100, 9]
Explanation
- set1 stores the unique elements of the first array.
- set2 stores the unique elements of the second array.
- set1.retainAll(set2) removes every element from set1 that is not present in set2.
- Therefore, set1 contains only the common elements.
Note: HashSet does not guarantee insertion order, so the order of elements in the output may vary.
3. Using HashSet and contains()
This approach uses a HashSet to efficiently check whether elements of the second array are present in the first array.
Approach
- Add all elements of the first array to a HashSet.
- Traverse the second array.
- Use contains() to check whether the current element exists in the set.
- Add matching elements to a result set.
- The result set prevents duplicate common elements.
import java.util.HashSet;
import java.util.Set;
public class Geeks {
public static void findCommonElements(int[] arr1, int[] arr2) {
Set<Integer> set = new HashSet<>();
Set<Integer> common = new HashSet<>();
// Add elements of the first array
for (int value : arr1) {
set.add(value);
}
// Find elements common to both arrays
for (int value : arr2) {
if (set.contains(value)) {
common.add(value);
}
}
System.out.println(common);
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5, 6, 7};
int[] arr2 = {1, 3, 4, 5, 6, 9, 8};
findCommonElements(arr1, arr2);
}
}
Output
[1, 3, 4, 5, 6]
Explanation
- A HashSet stores all unique elements from arr1.
- The second array is traversed one element at a time.
- contains() checks whether the current element exists in the first array.
- If it exists, it is added to the common set.
- Since common is a HashSet, duplicate elements are stored only once.