import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* 得到2个数组的交集
* @Package
* @ClassName: d
* @author: Wangj
*/
public class d {
/*
* 方法一
*/
public static int firstUniqChar(String s) {
HashMap<Character, Integer> count = new HashMap<Character, Integer>();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
System.out.println("s.charAt("+i+")"+s.charAt(i));
count.put(c, count.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < n; i++) {
if (count.get(s.charAt(i)) == 1)
return i;
}
return -1;
}
/*
* 方法二
*/
public static List<Integer> intersection(int [] nums1,int [] nums2) {
List<Integer> index = new ArrayList<Integer>();
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i]==nums2[j]) {
if (index.size()!=0) {
for (int x = 0; x < index.size(); x++) {
if (index.get(x)!=nums1[i]) {
index.add(nums1[i]);
}
}
}else{
index.add(nums1[i]);
}
}
}
}
List<Integer> newlist = index.stream().distinct().collect(Collectors.toList());
System.out.println("newlist:"+newlist.toString());
return index;
}
public static void main(String[] args) {
// int index = firstUniqChar("bcdbac");
// System.out.println("index"+index);
int[] num1 = {4,9,5,2,1,5,8};
int[] num2 = {9,4,9,8,4,5};
intersection(num1,num2);
}
}