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

来源:互联网 发布:淘宝电脑版官方下载 编辑:程序博客网 时间:2024/05/21 09:38

 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) + " ");
    }
   }
  }
 }

}

原创粉丝点击