Last Updated : 23 Jul, 2025
Try it on GfG Practice
Given two arrays and our task is to find their common elements.
Examples:Example to Find Common Elements Between Two Arrays JavaInput: 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]
// Java Program to find common elements
// in two Arrays
// Using iterative method
import java.io.*;
import java.util.*;
// Driver Class
class GFG {
// Function to Find Common Elements
private static void FindCommonElement(String[] arr1,String[] arr2)
{
Set<String> set = new HashSet<>();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i].equals(arr2[j])) {
// add common elements
set.add(arr1[i]);
break;
}
}
}
// Common Elements in Arrays
System.out.println(set.toString());
}
// Main Method
public static void main(String[] args)
{
// Create Array 1
String[] arr1 = { "Article", "in", "Geeks", "for", "Geeks" };
// Create Array 2
String[] arr2 = { "Geeks", "for", "Geeks" };
// Find the Common Elements
FindCommonElement(arr1, arr2);
}
}
Below is the explanation of the above Method:
Other Methods to Find the Common Elements Between Arrays 1. Using retainAll() method of HashSetTime Complexity: O(m * n) where m and n are lengths of the input arrays.
Auxiliary Space: O(d) where d is the number of common elements.
By using the retainAll() method of the HashSet we can find the common elements between two arrays.
Syntax:
// This method keeps only the common elements
// of both Collection in Collection1.
Collections1.retainAll(Collections2)
Approach :
Below is the implementation of the above approach:
Java
// Java Program to find common elements
// in two Arrays using hashsets
// and retainAll() method
import java.io.*;
import java.util.*;
class GFG {
// function to create hashsets
// from arrays and find
// their common element
public static void FindCommonElements(int[] arr1,
int[] arr2)
{
// create hashsets
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
// Adding elements from array1
for (int i : arr1) {
set1.add(i);
}
// Adding elements from array2
for (int i : arr2) {
set2.add(i);
}
// use retainAll() method to
// find common elements
set1.retainAll(set2);
System.out.println(set1);
}
// main method
public static void main(String[] args)
{
// create Array 1
int[] arr1 = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 };
// create Array 2
int[] arr2 = { 100, 9, 64, 7, 36, 5, 16, 3, 4, 1 };
FindCommonElements(arr1, arr2);
}
}
[16, 64, 1, 4, 36, 100, 9]Complexity of the above method:
2. Using HashSetTime Complexity: O(m + n)
Auxiliary Space: O(m + n)
Approach:
Below is the implementation of the above approach:
Java
import java.util.HashMap;
public class CommonElementsOfArrays {
public static void main(String[] args)
{
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7 };
int[] arr2 = new int[] { 1, 3, 4, 5, 6, 9, 8 };
findCommonElements(arr1, arr2);
/* expected output {1,3,4,5,6}
coming from the above written code
that is right if not please correct me
*/
}
public static void findCommonElements(int arr1[],
int arr2[])
{
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < arr1.length; i++) {
if (hashMap.containsKey(arr1[i])) {
hashMap.put(arr1[i],
hashMap.get(arr1[i]) + 1);
}
else {
hashMap.put(arr1[i], 1);
}
}
System.out.print("[ ");
for (int i = 0; i < arr2.length; i++) {
if (hashMap.containsKey(arr2[i])) {
hashMap.remove(arr2[i]);
// remove common elements from hashmap
// to avoid duplicates*/
System.out.print(arr2[i] + " ");
}
}
System.out.print("]");
}
}
Complexity of the above Method:
Time Complexity: O(m + n)
Auxiliary Space: O(m)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4