Java,从两个数组中查找重复和唯一的整数

时间:2015-04-20 01:55:50

标签: java arrays

我正在传递两个整数数组,并尝试查找重复项和唯一值 但我的输出看起来像这样

[]
[18, 2, 5, 1, 7, 2]

我做错了什么?

import java.util.*;
//unique and duplicate of two arrays
class uniqueDup {
    public int dupUnique(int[] array1, int[] array2){
        ArrayList<Integer> unique = new ArrayList<>();    
        ArrayList<Integer> dup = new ArrayList<>();
        for(int i=0; i<array1.length+1; i++){
            for(int j=0; j<array2.length+1; j++){
                if(array1[i] == array2[j]){
                    dup.add(array1[i]);
                } else {
                    unique.add(array1[i]);
                }
            }
        }
        System.out.println(dup);
        System.out.println(unique);
        return 0;
    }

    public static void main(String args[]){
        uniqueDup test = new uniqueDup();
        int arraya[] = {18,2,5,1,7,2,4};
        int arrayb[] = {18,1,44,1,22,124,1,21};
        test.dupUnique(arraya,arrayb);
    }
}

3 个答案:

答案 0 :(得分:0)

    public int dupUnique(int[] array1, int[] array2){
    ArrayList<Integer> unique = new ArrayList<>();    
    ArrayList<Integer> dup = new ArrayList<>();
    for(int i=0; i<array1.length; i++){
        boolean duplicate = false;
        for(int j=0; j<array2.length; j++){
            if(array1[i] == array2[j]){
                dup.add(array1[i]);
                duplicate = true;
                break;
            }
        }
        if (!duplicate) {
            unique.add(array1[i]);
        }
    }
    System.out.println(dup);
    System.out.println(unique);
    return 0;
}

答案 1 :(得分:0)

不同的方法:您可以将所有值插入HashMapKey作为IntegerValue作为计数。从Entry<Integer, Integer>开始,您不仅可以识别重复项和唯一条目,还可以识别计数:

public static Set<Entry<Integer, Integer>> findDup(int [] a, int [] b) {
    HashMap<Integer, Integer> entries = new HashMap<>();

    for (Integer i:a) 
        entries.put(i, entries.get(i) == null ? 1 : entries.get(i) + 1);

    for (Integer i:b) 
        entries.put(i, entries.get(i) == null ? 1 : entries.get(i) + 1);


    return entries.entrySet();
}

entries.put()将每个条目放在哈希映射中,值为:

  • 1(尚未包含在地图中的项目)
  • count + 1(项目已在地图中)

然后您可以找到重复项和唯一条目,如下所示:

for (Entry<Integer, Integer> entry: findDup(a, b)) 
    if (entry.getValue() > 1) 
        System.out.println("duplicate: " + entry.getKey() + " (count = " + entry.getValue() + ")");
    else 
        System.out.println("unique: " + entry.getKey() );

答案 2 :(得分:0)

import java.util.ArrayList;
import java.util.Arrays;

public class ArraysUniqueDupNumbersOrder {
    public static void main(String args[]) {
        int[] array1 = {18,2,5,1,7,2,4};
        int[] array2 = {18,1,44,1,22,124,1,21};
        ArrayList<Integer> unique = new ArrayList<>();
        ArrayList<Integer> dup = new ArrayList<>();

        // get the duplicate (common) numbers
        // get the unique numbers of array1 when compared to array2
        for(int i=0; i<array1.length; i++){
            boolean duplicate = false;
            for(int j=0; j<array2.length; j++){
                if(array1[i] == array2[j]){
                    dup.add(array1[i]);
                    duplicate = true;
                    break;
                }
            }
            if (!duplicate) {
                unique.add(array1[i]);
            }
        }

        // Add the unique numbers of array2 when compared to array1
        for(int j=0; j<array2.length; j++){
            boolean duplicate = false;
            for(int i=0; i<array1.length; i++){
                if(array1[i] == array2[j]){
                    duplicate = true;
                    break;
                }
            }
            if (!duplicate) {
                unique.add(array2[j]);
            }
        }

        System.out.println("Given two arrays: "+Arrays.toString(array1) + " and "+Arrays.toString(array2));
        System.out.println("Duplicates are: "+dup);
        System.out.println("Unique elements in the order from 2 arrays: "+unique);
    }

}

输出: - 鉴于两个阵列:[18,2,5,1,7,2,4]和[18,1,44,1,22,124,1,21]

重复是:[18,1]

2个数组的顺序中的唯一元素:[2,5,7,2,4,42,22,124,21]

github

中提供了此代码的更多详细信息