nielarshi
BAN USERimport java.util.*;
import java.lang.*;
import java.io.*;
class CommonElement
{
private static int arrayOfArrays[][] = {
{1, 5, 4, 3},
{3, 4, 5, 10, 6, 8},
{5, 10, 20, 4, 3},
{5, 4, 10, 20, 12},
{2, 4, 10, 5}
};
public static ArrayList<Integer> presentElements = new ArrayList<Integer>();
public static int[] getSmallestSizedArray() {
int smallestSize = getSmallestArraySize();
int[] smallestArray = null;
for(int i = 0; i< arrayOfArrays.length; i++) {
if(arrayOfArrays[i].length == smallestSize) {
smallestArray = arrayOfArrays[i];
}
}
return smallestArray;
}
private static int getSmallestArraySize() {
ArrayList<Integer> arrays = new ArrayList<Integer>();
for(int i = 0; i< arrayOfArrays.length; i++) {
arrays.add(arrayOfArrays[i].length);
}
Collections.sort(arrays);
int smallest = arrays.get(0);
return smallest;
}
public static void checkInArray(int index, int element) {
for(int i = 0; i< arrayOfArrays[index].length; i++) {
if(arrayOfArrays[index][i] == element) {
if(index == arrayOfArrays.length-1) {
presentElements.add(element);
} else {
checkInArray(index+1, arrayOfArrays[index][i]);
}
}
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] smallestArray = getSmallestSizedArray();
for(int i = 0; i< smallestArray.length; i++) {
checkInArray(0, smallestArray[i]);
}
System.out.println(presentElements.toString());
}
}
- nielarshi March 18, 2015