-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray-03
More file actions
77 lines (54 loc) · 1.57 KB
/
Copy pathArray-03
File metadata and controls
77 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*Sheela wants to find the common data in the two different lists. Could you please help to find it and implement the program. Both lists are non Duplicate data
Note-if the data are not found from the list return the Statement Data Not found.
Input Format
First Input Corresponds to the array size. Second Input Corresponds to the array elements of arr1. Third Input corresponds to the array elements of arr2.
Constraints
No Constraints
Output Format
Find the common datas from the two lists.
Sample Input 0
4
a b e f
e d h a
Sample Output 0
The Common data are a e
Sample Input 1
4
m o d g
a b c e
Sample Output 1*
#Answer
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr1 = new String[n];
String[] arr2 = new String[n];
for(int i = 0; i < n; i++){
arr1[i] = sc.next();
}
for(int i = 0; i < n; i++){
arr2[i] = sc.next();
}
List<String> common = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(arr1[i].equals(arr2[j])){
common.add(arr1[i]);
}
}
}
if(common.isEmpty()){
System.out.println("Data Not Found");
} else {
System.out.print("The Common data are ");
for(String s : common){
System.out.print(s + " ");
}
}
sc.close();
}
}
Cont