Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if

阅读 167

2022-08-01


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list).


import java.util.ArrayList;
import java.util.List;public class Duplicate {
public static void main(String args[]) {
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>(); for (int i = 0; i < 5; i++)
list1.add((int) (Math.random() * 10)); for (int i = 0; i < 10; i++)
list2.add((int) (Math.random() * 10)); for (int i = 0; i < list1.size(); i++) {
System.out.print(list1.get(i) + " ");
}
System.out.println();
for (int j = 0; j < list2.size(); j++) {
System.out.print(list2.get(j) + " ");
}
System.out.println();
for (int i = 0; i < list2.size(); i++) {
for (int j = 0; j < list1.size(); j++) {
if (list1.get(j) == list2.get(i)) { System.out.print(list2.get(i) + " ");
}
}
}
}}

精彩评论(0)

0 0 举报