1. Find Union and Intersection of two sets. Given two sets (elements in a set ar
ID: 3589866 • Letter: 1
Question
1. Find Union and Intersection of two sets. Given two sets (elements in a set are distinct), write a method that finds the union and intersection of two sets using linked list. E.g., given A= {6,1,3, 20,7 }, B={2,6,8,3,5} your algorithm should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the elements of union and intersection can be printed in any order. (java code)
2. Write a method rotate(list,k) which rotates an integer list by a given number k. k elements from the end should move to the beginning of the list, and all other elements should move to right to make the space. E.g., if list a is initialized with elements l = {1, 2, 3, 4, 5, 6, 7, 8, 9}. (java code)
Explanation / Answer
Please uncomment the commented code from below program if you would like to use linked list for problem 2 also.
import java.util.*;
import java.util.List;
public class LinkedList {
public static void main(String[] args) {
java.util.LinkedList<Integer> set_A = new java.util.LinkedList<Integer>();
java.util.LinkedList<Integer> set_B = new java.util.LinkedList<Integer>();
// java.util.LinkedList<Integer> rotate_linked_list = new java.util.LinkedList<Integer>();
List<Integer> rotate_list = new ArrayList<Integer>();
set_A.add(6);
set_A.add(1);
set_A.add(3);
set_A.add(20);
set_A.add(7);
set_B.add(2);
set_B.add(6);
set_B.add(8);
set_B.add(3);
set_B.add(5);
rotate_list.add(1);
rotate_list.add(2);
rotate_list.add(3);
rotate_list.add(4);
rotate_list.add(5);
rotate_list.add(6);
rotate_list.add(7);
rotate_list.add(8);
rotate_list.add(9);
/* rotate_linked_list.add(1);
rotate_linked_list.add(2);
rotate_linked_list.add(3);
rotate_linked_list.add(4);
rotate_linked_list.add(5);
rotate_linked_list.add(6);
rotate_linked_list.add(7);
rotate_linked_list.add(8);
rotate_linked_list.add(9); */
/*Display Linked List Content*/
System.out.println("Set A Content: " +set_A);
System.out.println("Set B Content: " +set_B);
System.out.println("Rotate List content: " +rotate_list);
UnionAndIntersection(set_A,set_B);
rotate(rotate_list, 2);
// rotateLinkedList(rotate_linked_list, 3);
}
public static void UnionAndIntersection(java.util.LinkedList<Integer> A, java.util.LinkedList<Integer> B) {
java.util.LinkedList<Integer> union_set = new java.util.LinkedList<Integer>();
java.util.LinkedList<Integer> intersection_set = new java.util.LinkedList<Integer>();
union_set.addAll(A);
for (Integer element : B) {
if(!A.contains(element)) {
union_set.add(element);
}else {
intersection_set.add(element);
}
}
System.out.println("A Union B Content: " +union_set);
System.out.println("A Intersection B Content: " +intersection_set);
}
public static void rotate(List<Integer> list,int k) {
Collections.rotate(list, k);
System.out.println("Rotated List content: " +list);
}
/* public static void rotateLinkedList(java.util.LinkedList<Integer> list,int k) {
Collections.rotate(list, k);
System.out.println("Rotated Linked List content: " +list);
} */
}
Kindly let me know if you need more information.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.