KaiquanMah commited on
Commit
54803e1
·
verified ·
1 Parent(s): 99aaa37

Create 20. Doubles

Browse files
Week 2: Methods, strings and lists/20. Doubles ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write the method
2
+
3
+ int doubles(ArrayList<Integer> list1, ArrayList<Integer> list2)
4
+
5
+ which takes two integer reading lists as parameters.
6
+ The method returns the number of elements found in both list 1 and list 2.
7
+
8
+
9
+ Example method call:
10
+ public static void main(String[] args){
11
+ ArrayList<Integer> list1 = new ArrayList<>();
12
+ ArrayList<Integer> list2 = new ArrayList<>();
13
+
14
+ list1.add(1);
15
+ list1.add(2);
16
+ list1.add(3);
17
+
18
+ list2.add(3);
19
+ list2.add(4);
20
+ list2.add(5);
21
+ list2.add(6);
22
+
23
+ int t = doubles(list1, list2);
24
+ System.out.println("Doubles: " + t);
25
+ }
26
+
27
+ Program outputs:
28
+ Doubles: 1
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+ import java.util.Random;
40
+ import java.util.ArrayList;
41
+ import java.util.Arrays;
42
+ import java.util.stream.Collectors;
43
+
44
+ public class Test{
45
+ public static void main(String[] args){
46
+ final Random r = new Random();
47
+
48
+ test(new int[]{1,2,3,4,5,6,7,8}, new int[]{5,6,7,8,9,10,11,12});
49
+ test(new int[]{10,20,30,40,50,60,70}, new int[]{25,30,35,40,45,50});
50
+ test(new int[]{1,9,2,8,3,7,4,6}, new int[]{10,11,12,3,14,15,16,7,4,99,1,98,2});
51
+ }
52
+
53
+ public static void test(int[] l1, int[] l2) {
54
+ ArrayList<Integer> list1 = new ArrayList<>();
55
+ for (int a : l1) {
56
+ list1.add(a);
57
+ }
58
+
59
+ ArrayList<Integer> list2 = new ArrayList<>();
60
+ for (int a : l2) {
61
+ list2.add(a);
62
+ }
63
+
64
+ System.out.println("Testing with lists ");
65
+ System.out.println("" + list1);
66
+ System.out.println("and");
67
+ System.out.println("" + list2);
68
+ System.out.println("Doubles: " + doubles(list1, list2));
69
+ System.out.println("");
70
+ }
71
+
72
+
73
+
74
+ //ADD
75
+ //ASSUME BOTH list1, list2 do not contain dupe elements
76
+ public static int doubles(ArrayList<Integer> list1, ArrayList<Integer> list2) {
77
+ int count = 0;
78
+
79
+ // // check Double dtype of each element
80
+ // for (int i = 0; i < list1.size(); i++) {
81
+ // if (list1.get(i) instanceof Double) {
82
+ // count++;
83
+ // }
84
+ // }
85
+ // for (int i = 0; i < list2.size(); i++) {
86
+ // if (list2.get(i) instanceof Double) {
87
+ // count++;
88
+ // }
89
+ // }
90
+
91
+ for (int i: list1) {
92
+ if (list2.contains(i)) {
93
+ count++;
94
+ }
95
+ }
96
+ return count;
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+ }
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+ Testing with lists
113
+ [1, 2, 3, 4, 5, 6, 7, 8]
114
+ and
115
+ [5, 6, 7, 8, 9, 10, 11, 12]
116
+ Doubles: 4
117
+
118
+ Testing with lists
119
+ [10, 20, 30, 40, 50, 60, 70]
120
+ and
121
+ [25, 30, 35, 40, 45, 50]
122
+ Doubles: 3
123
+
124
+ Testing with lists
125
+ [1, 9, 2, 8, 3, 7, 4, 6]
126
+ and
127
+ [10, 11, 12, 3, 14, 15, 16, 7, 4, 99, 1, 98, 2]
128
+ Doubles: 5
129
+
130
+