Java Program to Find Common Elements in Two Arrays
HashSet to store elements of one array and then checking which elements of the second array are contained in that set, like this: Set set = new HashSet<>(); for (int num : array1) { set.add(num); } for (int num : array2) { if (set.contains(num)) { /* common element */ } } .Examples
How to Think About It
Algorithm
Code
import java.util.*; public class CommonElements { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = {3, 4, 5, 6, 7}; Set<Integer> set = new HashSet<>(); for (int num : array1) { set.add(num); } System.out.print("Common elements: "); for (int num : array2) { if (set.contains(num)) { System.out.print(num + " "); } } } }
Dry Run
Let's trace the example arrays {1, 2, 3, 4, 5} and {3, 4, 5, 6, 7} through the code.
Add elements of first array to set
Set after adding: {1, 2, 3, 4, 5}
Check elements of second array against set
Check 3 (in set), 4 (in set), 5 (in set), 6 (not in set), 7 (not in set)
Print common elements
Output: 3 4 5
| Element in array2 | Is in set? | Action |
|---|---|---|
| 3 | Yes | Print 3 |
| 4 | Yes | Print 4 |
| 5 | Yes | Print 5 |
| 6 | No | Skip |
| 7 | No | Skip |
Why This Works
Step 1: Use a set for fast lookup
A HashSet stores elements so you can check if an item exists quickly, unlike searching an array which takes longer.
Step 2: Add first array elements to the set
Putting all elements of the first array into the set prepares for quick membership checks.
Step 3: Check second array elements against the set
For each element in the second array, checking if it is in the set tells us if it is common.
Alternative Approaches
public class CommonElementsNested { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {2, 3, 4}; System.out.print("Common elements: "); for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array2.length; j++) { if (array1[i] == array2[j]) { System.out.print(array1[i] + " "); break; } } } } }
import java.util.Arrays; public class CommonElementsSorted { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4}; int[] array2 = {3, 4, 5, 6}; Arrays.sort(array1); Arrays.sort(array2); int i = 0, j = 0; System.out.print("Common elements: "); while (i < array1.length && j < array2.length) { if (array1[i] == array2[j]) { System.out.print(array1[i] + " "); i++; j++; } else if (array1[i] < array2[j]) { i++; } else { j++; } } } }
Complexity: O(n + m) time, O(n) space
Time Complexity
Adding all elements of the first array to a HashSet takes O(n). Checking each element of the second array against the set takes O(m). Total is O(n + m).
Space Complexity
The HashSet stores up to n elements from the first array, so space is O(n).
Which Approach is Fastest?
Using a HashSet is generally fastest for unsorted arrays. Nested loops are slow (O(n*m)). Sorting and two pointers is good if arrays are already sorted or sorting is acceptable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| HashSet | O(n + m) | O(n) | Unsorted arrays, fast lookup |
| Nested loops | O(n * m) | O(1) | Very small arrays or no extra space |
| Sorting + Two pointers | O(n log n + m log m) | O(1) | Sorted arrays or when sorting is allowed |