CC-11 : Find integers that are common in 2 out of 3 arrays.
Description:
Given 3 integer arrays arr1, arr2, arr3, find and return integers that are common in all 3 arrays.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| intArray1 = {1,2,3,4,5,6,7,8}; intArray2 = {8, 9,10,11,12}; intArray3 = {14,15,16,17}; |
Common elements in atleast 2 arrays: 8 |
| intArray4 = {-3,-2,-1,0,1,2,3}; intArray5= {-2,-1,0, 1, 2}; intArray6= {-2,-1,0,4,5}; |
Common elements in atleast 2 arrays: : -1,0,-2,1,2 |
Pseudocode:
| The java method should accept following input parameters: arr1(integer array), arr2(integer array), arr3(integer array). |
| Initialize 4 marker integer constants PRESENT_IN_1st_ARR=1; PRESENT_IN_2nd_ARR=2; PRESENT_IN_3rd_ARR=3; PRESENT_IN_ATLEAST2ARRAYS=4. |
| Initialize idx to 0. Initialize commonCnt to 0. |
| Initialize a variable common of type HashMap |
| Iterate through arr1 using a for loop with idx as a loop variable with initial value 0 and increment idx till it reaches arr1.length-1:
add arr1[idx] as key and PRESENT_IN_1st_ARR as value in common.
|
| Iterate through arr2 using a for loop with idx as a loop variable with initial value 0 and increment idx till it reaches arr2.length-1:
If common does not contain key arr2[idx], then put key-value pair arr2[idx] and PRESENT_IN_2nd_ARR to common.
If common contains key arr2[idx] with value PRESENT_IN_1st_ARR, then put key-value pair arr2[idx] and PRESENT_IN_ATLEAST2ARRAYS to common.
|
| Iterate through arr3 using a for loop with idx as a loop variable with initial value 0 and increment idx till it reaches arr3.length-1:
If common does not contain key arr3[idx], then put key-value pair arr3[idx] and PRESENT_IN_3rd_ARR to common.
If common contains key arr3[idx] with value PRESENT_IN_1st_ARR or PRESENT_IN_2nd_ARR, then put key-value pair arr3[idx] and PRESENT_IN_ATLEAST2ARRAYS to common.
|
| After above loop completes successfully, iterate through keys of common and identify keys that have value PRESENT_IN_ATLEAST2ARRAYS and move these keys to an integer array commonArr. Return commonArr. |
Code:
public int[] hashMapCommonIn2OutOf3Arrays(int[] arr1,int[] arr2,int[] arr3) throws Exception{
int PRESENT_IN_1st_ARR=1;
int PRESENT_IN_2nd_ARR=2;
int PRESENT_IN_3rd_ARR=3;
int PRESENT_IN_ATLEAST2ARRAYS=4;
int idx=0, commonCnt=0;
HashMap <Integer, Integer> common=new HashMap <Integer, Integer>();
for (idx=0; idx < arr1.length; idx++) {
common.put(arr1[idx],PRESENT_IN_1st_ARR);
}
int chkCommon=0;
for (idx=0; idx < arr2.length; idx++) {
chkCommon=common.getOrDefault(arr2[idx],0);
if (chkCommon==0) {
common.put(arr2[idx],PRESENT_IN_2nd_ARR);
}else {
if (chkCommon==PRESENT_IN_1st_ARR) {
common.put(arr2[idx],PRESENT_IN_ATLEAST2ARRAYS);
commonCnt++;
}
}
}
chkCommon=0;
for (idx=0; idx < arr3.length; idx++) {
chkCommon=common.getOrDefault(arr3[idx],0);
if (chkCommon==0) {
common.put(arr3[idx],PRESENT_IN_3rd_ARR);
}else {
if ((common.get(arr3[idx])==PRESENT_IN_1st_ARR)||
(common.get(arr3[idx])==PRESENT_IN_2nd_ARR)) {
common.put(arr3[idx],PRESENT_IN_ATLEAST2ARRAYS);
commonCnt++;
}
}
}
int[] commonArr=new int[commonCnt];
Iterator itr=common.keySet().iterator();
int num=0; idx=0;
while (itr.hasNext()) {
num=((Integer)itr.next()).intValue();
chkCommon=common.get(num);
if (chkCommon==PRESENT_IN_ATLEAST2ARRAYS) {
commonArr[idx]=num;
idx++;
}
}
return commonArr;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |