From c2760942807ac91918b07cf4f74e5b159a924eff Mon Sep 17 00:00:00 2001 From: Gaurav Shah <100478378+Gauravshah001@users.noreply.github.com> Date: Wed, 19 Oct 2022 22:22:50 +0530 Subject: [PATCH] to find two common elements in the array --- to find common elements between array | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 to find common elements between array diff --git a/to find common elements between array b/to find common elements between array new file mode 100644 index 0000000..9a76ef7 --- /dev/null +++ b/to find common elements between array @@ -0,0 +1,33 @@ +import java.io.*; +import java.util.*; + +class GFG { + private static void FindCommonElemet(String[] arr1, + String[] arr2) + { + Set set = new HashSet<>(); + for (int i = 0; i < arr1.length; i++) { + for (int j = 0; j < arr2.length; j++) { + if (arr1[i] == arr2[j]) { + set.add(arr1[i]); + break; + } + } + } + for (String i : set) { + System.out.print(i + " "); + } + } + public static void main(String[] args) + { + String[] arr1 + = { "Article", "in", "Geeks", "for", "Geeks" }; + String[] arr2 = { "Geeks", "for", "Geeks" }; + System.out.println("Array 1: " + + Arrays.toString(arr1)); + System.out.println("Array 2: " + + Arrays.toString(arr2)); + System.out.print("Common Elements: "); + FindCommonElemet(arr1, arr2); + } +}