Wednesday, 15 July 2015

Java Programs Made Easy

Q)...Get two integer arrays as input. Find if there are common elements in the arrays. find the number of common elements.
input1: {1,2,3,4}
input2: {3,4,5,6}
output: 4(3,4,3,4)



  1. package Set1;
  2. public class ClassSet22 {
  3.  public static int commonElements(int[] a,int[] b){
  4.   int c=0;
  5.   for(int i=0;i<a.length;i++)
  6.    for(int j=0;j<b.length;j++)
  7.     if(a[i]==b[j])
  8.      c++;
  9.   return (2*c);
  10.  }
  11.  public static void main(String[] args){
  12.   int a[]={1,2,3,4,5};
  13.   int b[]={3,4,5,6,7};
  14.   System.out.println(commonElements(a,b));
  15.  }
  16. }