Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code, which finds the common values of 3 arrays. I need code that wi

ID: 3850888 • Letter: I

Question

I have this code, which finds the common values of 3 arrays. I need code that will find the common elements of an input of n number arrays. I believe a recursive code would work best, but I cannot figure one out.

    // This function prints common elements in ar1

    void findCommon(int ar1[], int ar2[], int ar3[])

    {

        // Initialize starting indexes for ar1[], ar2[] and ar3[]

        int i = 0, j = 0, k = 0;

        // Iterate through three arrays while all arrays have elements

        while (i < ar1.length && j < ar2.length && k < ar3.length)

        {

             // If x = y and y = z, print any of them and move ahead

             // in all arrays

             if (ar1[i] == ar2[j] && ar2[j] == ar3[k])

             {   System.out.print(ar1[i]+" ");   i++; j++; k++; }

             // x < y

             else if (ar1[i] < ar2[j])

                 i++;

             // y < z

             else if (ar2[j] < ar3[k])

                 j++;

             // We reach here when x > y and z < y, i.e., z is smallest

             else

                 k++;

        }

    }

I need to know how to change the code so I can compare n number of arrays at a time. Or if theres and entirely different code to do what I want.

Explanation / Answer

The following method can be used to find the common elements in n number of arrays(I have not modified your code but instead i have given proper function its explanation and program implementation).

Function -

for i = 1 to N:

x_i = 1

for x_1 = 1 to N:
val = A_1[x_1]
print_val = true
for i = 2 to N:
while A_i[x_i] < val:
x_i = x_i + 1
if A_i[x_i] != val:
print_val = false
if print_val:
print val

For finding common value among n arrays we will do it by taking element of first array(reference array) and will match it with all the elements of other arrays.We will use the first array as the reference array and will traverse all other arrays in parallel way.The reference array is represented by x_i and its first element by x_1. The common value should lie in reference array as well as in the other arrays too.So for each iteration or completion of cycle we increase the index of x_i we have traversed all the arrays i.e. A_i[x_i].If the arrays have any common values it we will print those values otherwise x_1 will be incremented i.e. we will select the second element of reference array and the process will keep going until all arrays are checked.

And for ease here is the program implementation.You can enter as many number of arrays you want -

public static void main(String[] args) {

int[][] a = {
               // enter arrays here in form of {1,2,3,4,5},{4,6,8,9,21,1}
           };

int n = a.length;
int[] x = new int[n];

for( ; x[0] < n; x[0]++ ) {
int val = a[0][x[0]];
boolean print = true;
for( int i = 1; i < n; i++ ) {
while (a[i][x[i]] < val && x[i] < n-1) x[i]++;
if (a[i][x[i]] != val) print = false;   
}   
if (print) System.out.println(val);
}   
}

Kindly rate my answer.ThankYou.