MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / commonElements

Method commonElements

CommanElements.java:3–32  ·  view source on GitHub ↗
(int A[], int B[], int C[], int n1, int n2, int n3)

Source from the content-addressed store, hash-verified

1class Solution
2{
3 ArrayList<Integer> commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
4 {
5 // code here
6 // Declare an Array list to store results
7 ArrayList<Integer> arr = new ArrayList<>();
8 int i=0,j=0,k=0;
9 while(i<n1 && j<n2 && k<n3)
10 {
11 if(A[i]<B[j]) i++;
12 else if(A[i]>B[j]) j++;
13 else
14 {
15 // This is for checking duplicacy
16 if(i>0 && A[i]==A[i-1])
17 {
18 i++;
19 continue;
20 }
21 // Now we will search the common element in third array!
22 // k<n3 avoids array index out of bound exception
23 while(k<n3 && C[k]<B[j]) k++;
24 if(k<n3 && C[k]==B[j])
25 {
26 arr.add(C[k]);
27 }
28 i++;j++;
29 }
30 }
31 return arr;
32 }
33}

Callers

nothing calls this directly

Calls 1

addMethod · 0.45

Tested by

no test coverage detected