Given 3 input integer arrays arr1, arr2, arr3, that are sorted in ascending order. Find the common elements that occur in all 3 arrays.
| Input Parameters |
Expected outputs |
arr1={-3,-2,-1,0,1,2,3}
arr2={-2,-1,0}
arr3={-2,-1,0,4,5}
|
Common Element in the 3 arrays : -2.
Common Element in the 3 arrays : -1.
Common Element in the 3 arrays : 0.
|
arr1={4,5}
arr2={1,2,4,5}
arr3={1,2,4,5,6,7}
|
Common Element in the 3 arrays : 4.
Common Element in the 3 arrays : 5.
|
arr1={1,2,3}
arr2={2,4}
arr3={3,5}
|
*No* common element in the 3 arrays !
|
| The java method should accept following input parameters: arr1 (int array), arr2 (int array), arr3 (int array).
|
| Initialize a boolean variable noCmnElmt to true.
|
| Iterate through arr1 using while loop:
For each element of arr1, search the same in arr2 and arr3 using while loops. Since the arrays are sorted, while searching arr2 and arr3, if you find an element greater than the current element of arr1, you may stop the current search as there is no further probability of finding the element.
If current element of arr1 is found in arr2 and arr3, print the same and set noCmnElmt to false.
|
| • If at the end of the loop noCmnElmt is still true, it means no common element exists in the 3 arrays.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayFindCommonElementsIn3Arrays {
public void arrayFindCommon(int[] arr1,int[] arr2,int[] arr3) throws Exception{
int arr1Idx=0;int arr2Idx=0;int arr3Idx=0;
boolean foundArr2; boolean foundArr3; boolean noCmnElmt=true;
while (arr1Idx < arr1.length) {
arr2Idx=0; arr3Idx=0;
foundArr2=false; foundArr3=false;
while ((arr2Idx < arr2.length)&&(arr1[arr1Idx]>=arr2[arr2Idx])) {
if (arr1[arr1Idx]==arr2[arr2Idx]) {
foundArr2=true;
arr2Idx++;
break;
}
arr2Idx++;
}
while ((arr3Idx < arr3.length)&&(arr1[arr1Idx]>=arr3[arr3Idx])) {
if (arr1[arr1Idx]==arr3[arr3Idx]) {
foundArr3=true;
arr3Idx++;
break;
}
arr3Idx++;
}
if (foundArr2==true && foundArr3==true) {
System.out.println("\nCommon Element in the 3 arrays : "+arr1[arr1Idx] +"\n");
noCmnElmt=false;
}
arr1Idx++;
}
if (noCmnElmt==true) {
System.out.println("\n*No* common element in the 3 arrays !\n");
}
}
public static void main(String[] args) {
ArrayFindCommonElementsIn3Arrays ap= new ArrayFindCommonElementsIn3Arrays();
try {
int[] intArray1 = {1,2,3,4,5,6,7,8};
int[] intArray2 = {9,10,11,12};
int[] intArray3 = {14,15,16,17};
printArraySummary(intArray1, "Original Array1");
printArraySummary(intArray2, "Original Array2");
printArraySummary(intArray3, "Original Array3");
ap.arrayFindCommon(intArray1, intArray2,intArray3 );
int[] intArray4 = {-3,-2,-1,0,1,2,3};
int[] intArray5= {-2,-1,0};
int[] intArray6= {-2,-1,0,4,5};
printArraySummary(intArray4, "Original Array1");
printArraySummary(intArray5, "Original Array2");
printArraySummary(intArray6, "Original Array3");
ap.arrayFindCommon(intArray4,intArray5,intArray6 );
int[] intArray7 = {4,5};
int[] intArray8 = {1,2,4,5};
int[] intArray9 = {1,2,4,5,6,7};
printArraySummary(intArray7, "Original Array1");
printArraySummary(intArray8, "Original Array2");
printArraySummary(intArray9, "Original Array3");
ap.arrayFindCommon(intArray7, intArray8,intArray9);
int[] intArray10 = {4,7};
int[] intArray11 = {4,7};
int[] intArray12 = {4,7};
printArraySummary(intArray10, "Original Array1");
printArraySummary(intArray11, "Original Array2");
printArraySummary(intArray12, "Original Array3");
ap.arrayFindCommon(intArray10,intArray11,intArray12);
int[] intArray13 = {1};
int[] intArray14 = {1};
int[] intArray15 = {1};
printArraySummary(intArray13, "Original Array1");
printArraySummary(intArray14, "Original Array2");
printArraySummary(intArray15, "Original Array3");
ap.arrayFindCommon(intArray13, intArray14,intArray15);
int[] intArray16 = {1,2,3};
int[] intArray17 = {2,4};
int[] intArray18 = {3,5};
printArraySummary(intArray16, "Original Array1");
printArraySummary(intArray17, "Original Array2");
printArraySummary(intArray18, "Original Array3");
ap.arrayFindCommon(intArray16, intArray17,intArray18);
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}