-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise5.java
More file actions
47 lines (40 loc) · 1.43 KB
/
Copy pathExercise5.java
File metadata and controls
47 lines (40 loc) · 1.43 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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/**
* 6.8.5. Exercise 5
*
* Consider the following source:
*
* int[] list1 = {1, 2, 3, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 10};
* int[] list2 = {2, 4, 8, 10, 12, 14, 16, 18, 20};
*
* Create an new ArrayList named intersection that contains only those items that
* occur in both lists. If a value is duplicated in either list and it occurs in
* both lists, it should only occur once in the intersection list. For the lists
* provided, your ArrayList should contain: 2 4 8 10
*/
public class Exercise5 {
public static void main(String[] args) {
int[] list1 = {1, 2, 3, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 10};
int[] list2 = {2, 4, 8, 10, 12, 14, 16, 18, 20};
ArrayList<Integer> intersection = new ArrayList<>();
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
// Add elements from list1 to set1
for (int num : list1) {
set1.add(num);
}
// Add elements from list2 to set2
for (int num : list2) {
set2.add(num);
}
// Find intersection of set1 and set2
set1.retainAll(set2);
// Add elements from set1 to intersection ArrayList
for (int num : set1) {
intersection.add(num);
}
System.out.println("intersection = " + intersection);
}
}