File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ //WAP to find the common elements in n arrays of unknown size.
2
+
3
+ import java.util.*;
4
+
5
+ class MyArray{
6
+ static int n=0;
7
+ static int com[]=new int[50];
8
+
9
+ public void insert(int x){
10
+ com[n++]=x;
11
+ }
12
+
13
+ public void find(int key, int arr[]){
14
+ for(int i=0; i<arr.length; i++){
15
+ if(arr[i]==key)
16
+ this.insert(key);
17
+ }
18
+ }
19
+
20
+ public void findCom(int key, int arr[]){
21
+ boolean found=false;
22
+ for(int i=0; i<arr.length; i++){
23
+ if(arr[i]==key)
24
+ found=true;
25
+ }
26
+ if(found==false)
27
+ this.delete(key);
28
+ }
29
+
30
+ public void delete (int key){
31
+ for(int i=0; i<n; i++)
32
+ if(com[i]>=key)
33
+ com[i]=com[i+1];
34
+ n--;
35
+ }
36
+ }
37
+
38
+
39
+ class Test{
40
+ public static void main(String args[]){
41
+ Scanner sc= new Scanner(System.in);
42
+ MyArray marr=new MyArray();
43
+ int m,i;
44
+ System.out.print("Enter the number of arrays: ");
45
+ m=sc.nextInt();
46
+ int arr[][]=new int[m][];
47
+ for(i=0; i<m; i++){
48
+ System.out.print("Enter the number of elements in array "+(i+1)+": ");
49
+ int x=sc.nextInt();
50
+ arr[i]=new int[x];
51
+ for(int j=0; j<x; j++){
52
+ arr[i][j]=sc.nextInt();
53
+ }
54
+ }
55
+
56
+ for(i=0; i<arr[0].length; i++)
57
+ marr.find(arr[0][i], arr[1]);
58
+
59
+ for(i=2; i<m; i++){
60
+ for(int j=0; j<MyArray.n; j++)
61
+ marr.findCom(MyArray.com[j],arr[i]);
62
+ }
63
+
64
+ System.out.print("The common elements are: ");
65
+ for(i=0; i<MyArray.n; i++)
66
+ System.out.print(MyArray.com[i]+" ");
67
+ }
68
+ }
You can’t perform that action at this time.
0 commit comments