import java.util.*; /** * 8. Carefully design an algorithm that takes two unsorted lists A and B (ArrayList) * as parameters and removes from list A all elements that do not appear in list B. The * algorithm returns the number of elements removed. Thus, no new list is created; instead, * the parameter list is modified. Do not use the predefined remove(Object) or retainAll() * operations. First, draw a diagram of the problem, then clearly write down the solution * principle in English. After that, start refining the solution principle into a more precise * algorithm in 1-3 steps, ensuring that the final solution principle is described very precisely * and can be mechanically converted into a working subroutine. Do not modify the input * list B. What is the time complexity of your algorithm? Could it be improved? Hint for * improvement: create a set (HashSet) from list B, and use contains -operaation. *
* 9. Write an algorithm that takes two unsorted lists A and B (ArrayList) as parameters and * removes from list A all elements that do not appear in list B. Do not use the predefined * retainAll() operation. What is the time complexity? How could the time complexity be * improved? You will get the most out of this task if you make it work in linear time by * utilizing set as a helper. Use operations that are efficient for ArrayList. *
* 10. Write an algorithm that takes two unsorted lists A and B (LinkedList) as parameters and
* removes from list A all elements that do not appear in list B. Do not use the predefined
* retainAll() operation. What is the time complexity? How could the time complexity be
* improved? You will get the most out of this task if you make it work in linear time by
* utilizing set as a helper. Use operations that are efficient for LinkedList.
*/
public class TRAI_24_t9_10_skeleton {
public static void main(String[] args) {
// input sizes
int n1 = 10;
if (args.length > 0)
n1 = Integer.parseInt(args[0]);
int n2 = n1 + 2;
if (args.length > 1)
n2 = Integer.parseInt(args[1]);
int len = 1; // string lengths
if (n1 > 30)
len = 2;
// random number seed
int seed = 42;
if (args.length > 2)
seed = Integer.parseInt(args[2]);
// use strings this time
ArrayList