获取int数组Java中具有重复项的Elements

时间:2019-02-18 10:19:14

标签: java arrays

这篇帖子“ Java + Count duplicates from int array without using any Collection or another intermediate Array”也是我在学校的书中的一个示例练习,但是我想做的是获取具有重复项的元素而不进行排序。

我要做的是先删除数组的重复项,仅获取唯一的元素,然后将其与原始数组进行比较,并计算发现该元素的次数。但是问题是它不能打印出重复的正确元素。

int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};

正确的输出应为:7, 1, 4

但它输出:7, 6, 1

这是我的代码:

//method for removing duplicates
public static int[] removeDuplicates(int[] n) {
    int limit = n.length;

    for(int i = 0; i < limit; i++) {
        for(int j = i + 1; j < limit; j++) {
            if(n[i] == n[j]) {
                for(int k = j; k < limit - 1; k++) {
                    n[k] = n[k + 1];
                }
                limit--;
                j--;
            }
        }
    }

    int[] uniqueValues = new int[limit];
    for(int i = 0; i < uniqueValues.length; i++) {
        uniqueValues[i] = n[i];
    }

    return uniqueValues;
}

//method for getting elements that has duplicates
public static int[] getDuplicatedElements(int[] n) {
    int[] nCopy = n.clone();
    int[] u = removeDuplicates(nCopy);
    int count = 0;
    int limit = u.length;

    for(int i = 0; i < u.length; i++) {
        for(int j = 0; j < n.length; j++) {
            if(u[i] == n[j]) {
                count++;
            }
        }

        if(count == 1) {
            for(int k = i; k < limit - 1; k++) {
                u[k] = u[k + 1];
            }
            limit--;
        }
        count = 0;
    }

    int[] duplicated =  new int[limit];
    for(int i = 0; i < duplicated.length; i++) {
        duplicated[i] = u[i];
    }

    return duplicated;
}

//main
public static void main(String[] args) {
    int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};

    //printing original values
    System.out.print(Arrays.toString(num));
    System.out.println();


    int[] a = getDuplicatedElements(num);
    System.out.print("Elements with Duplicates: " + Arrays.toString(a)); 
}

这里的代码有什么错误?请帮忙谢谢...

2 个答案:

答案 0 :(得分:1)

使用流时非常简单

int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};
List<Integer> list = Arrays.stream(num).boxed().collect(Collectors.toList());
list.stream().filter(i -> Collections.frequency(list, i) > 1)
    .collect(Collectors.toSet()).forEach(System.out::println);

答案 1 :(得分:1)

您有两个问题:

public static int[] getDuplicatedElements(int[] n) {
    int[] nCopy = n.clone();
    int[] u = removeDuplicates(nCopy);
    System.out.println ("unique " + Arrays.toString (u));
    int count = 0;
    int limit = u.length;

    for(int i = 0; i < limit; i++) { // you must use limit instead of u.length
                                     // in order for the loop to terminate
        for(int j = 0; j < n.length; j++) {
            if(u[i] == n[j]) {
                count++;
            }
        }

        if(count == 1) {
            for(int k = i; k < limit - 1; k++) {
                u[k] = u[k + 1];
            }
            limit--;
            i--; // you must decrement i after you find a unique element in u
                 // otherwise you'll be skipping elements in the u array
        }
        count = 0;
    }

    int[] duplicated =  new int[limit];
    for(int i = 0; i < duplicated.length; i++) {
        duplicated[i] = u[i];
    }

    return duplicated;
}

使用这些修复程序,您将获得预期的输出:

Elements with Duplicates: [7, 1, 4]