Java 集与扭曲集的交集

Java 集与扭曲集的交集,java,hashmap,runtime,Java,Hashmap,Runtime,所以我最近在一次技术面试中得到了以下问题,我觉得这很有趣 给定两个数组:A={1,2,2,4,5}和B={1,2,6},编写执行以下两个操作的代码:A-B{2,4,5}和B-A{6}。也就是说,在另一个数组中找到一个公共元素,并将其从原始数组中删除。如果B是{1,2},那么B-A就是{},因为A包含B的所有元素,B没有唯一的元素 我用下面的方法解决了这个问题,我希望看看是否有人对如何改进有任何建议 public static void main(String args[]) { Map hash

所以我最近在一次技术面试中得到了以下问题,我觉得这很有趣

给定两个数组:A={1,2,2,4,5}和B={1,2,6},编写执行以下两个操作的代码:A-B{2,4,5}和B-A{6}。也就是说,在另一个数组中找到一个公共元素,并将其从原始数组中删除。如果B是{1,2},那么B-A就是{},因为A包含B的所有元素,B没有唯一的元素

我用下面的方法解决了这个问题,我希望看看是否有人对如何改进有任何建议

public static void main(String args[]) {
Map hashtable = new HashMap();

int a[] = {1,1,2,4,5,6,6};
int b[] = {1,2,6};

ArrayList ba = new ArrayList();
ArrayList ab = new ArrayList(); 
int[] occurances = new int[a.length];
//int occurances = 0;
for (int i = 0; i < a.length; i++) {
    occurances[a[i]]++;
    hashtable.put(a[i], occurances[a[i]]);

}
//System.out.println(Arrays.toString(occurances));
System.out.println(hashtable);
//find BA
for (int i = 0; i < b.length; i++) {
    if(hashtable.containsKey(b[i])) {
        occurances[b[i]]--;
        hashtable.put(b[i], occurances[b[i]]);
    } else ba.add(b[i]);


}
for(int i = 0; i <a.length; i++) {
    if(hashtable.containsKey(a[i]) && occurances[a[i]] != 0) {
        ab.add(a[i]);
        occurances[a[i]]--;
        hashtable.put(a[i], occurances[a[i]]);

    }

}

System.out.println("AB = " + ab);
System.out.println("BA =" + ba);

 } 
 }
publicstaticvoidmain(字符串参数[]){
Map hashtable=新HashMap();
int a[]={1,1,2,4,5,6,6};
int b[]={1,2,6};
ArrayList ba=新的ArrayList();
ArrayList ab=新的ArrayList();
int[]发生率=新的int[a.长度];
//int发生率=0;
for(int i=0;i对于(inti=0;i来说,可以使用标准操作来完成

Set<Integer> a; //assume initialized with {1,2,4,5}
Set<Integer> b; //assume initialized with {1,2,6}

a.removeAll(b);
System.out.println(a);
如果您这样做:

b.removeAll(a);
System.out.println(b);
然后你会得到

[6]

您可以使用具有并集和交集功能的
Set

Integer a[] = {1, 2, 2, 4, 5};
Integer b[] = {1, 2, 6};

public void test() {
    // Grow the two sets from the arrays.
    Set<Integer> sa = Arrays.stream(a)
            .collect(Collectors.toCollection(TreeSet::new));
    Set<Integer> sb = Arrays.stream(b)
            .collect(Collectors.toCollection(TreeSet::new));
    // Make a new one so I don't damage sa or sb.
    Set<Integer> sc = new HashSet<>(sa);
    sc.removeAll(sb);
    System.out.println(sa + " - " + sb + " = " + sc);
    Set<Integer> sd = new HashSet<>(sb);
    sd.removeAll(sa);
    System.out.println(sb + " - " + sa + " = " + sd);
}

Map hashtable
…我的眼睛x_x
{1,2,2,4,5}
不是一个集合。建议使用java集合实现集合a和B。与通过列表实现集合相比,您的工作量要少。不幸的是,这对重复的集合不起作用。如果a是{1,2,2,3},B是{2,4},a-B应该是{1,2,3}只有在b中找到的单个2被删除。集合操作将删除a中所有出现的2。那么a={1,2,2,3}根据定义不是集合。我的错误。我猜它们只是数组。
Integer a[] = {1, 2, 2, 4, 5};
Integer b[] = {1, 2, 6};

public void test() {
    // Grow the two sets from the arrays.
    Set<Integer> sa = Arrays.stream(a)
            .collect(Collectors.toCollection(TreeSet::new));
    Set<Integer> sb = Arrays.stream(b)
            .collect(Collectors.toCollection(TreeSet::new));
    // Make a new one so I don't damage sa or sb.
    Set<Integer> sc = new HashSet<>(sa);
    sc.removeAll(sb);
    System.out.println(sa + " - " + sb + " = " + sc);
    Set<Integer> sd = new HashSet<>(sb);
    sd.removeAll(sa);
    System.out.println(sb + " - " + sa + " = " + sd);
}
[1, 2, 4, 5] - [1, 2, 6] = [4, 5]
[1, 2, 6] - [1, 2, 4, 5] = [6]